aboutsummaryrefslogtreecommitdiff
path: root/ext/mixed/js/extension.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-10-24 19:35:41 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-10-28 17:03:42 -0400
commit1039536cfb6279aadb9241c5905aa64790cd6fac (patch)
tree2e001d4bd1e60873121959d07bf9a550157817bb /ext/mixed/js/extension.js
parentf6d3f739a85367058a45581c7d7e4f9fffcca776 (diff)
Create promise version of setTimeout
Diffstat (limited to 'ext/mixed/js/extension.js')
-rw-r--r--ext/mixed/js/extension.js38
1 files changed, 38 insertions, 0 deletions
diff --git a/ext/mixed/js/extension.js b/ext/mixed/js/extension.js
index 861e52a5..54862e19 100644
--- a/ext/mixed/js/extension.js
+++ b/ext/mixed/js/extension.js
@@ -95,3 +95,41 @@ if (EXTENSION_IS_BROWSER_EDGE) {
// Edge does not have chrome defined.
chrome = browser;
}
+
+function promiseTimeout(delay, resolveValue) {
+ if (delay <= 0) {
+ return Promise.resolve(resolveValue);
+ }
+
+ let timer = null;
+ let promiseResolve = null;
+ let promiseReject = null;
+
+ const complete = (callback, value) => {
+ if (callback === null) { return; }
+ if (timer !== null) {
+ window.clearTimeout(timer);
+ timer = null;
+ }
+ promiseResolve = null;
+ promiseReject = null;
+ callback(value);
+ };
+
+ const resolve = (value) => complete(promiseResolve, value);
+ const reject = (value) => complete(promiseReject, value);
+
+ const promise = new Promise((resolve, reject) => {
+ promiseResolve = resolve;
+ promiseReject = reject;
+ });
+ timer = window.setTimeout(() => {
+ timer = null;
+ resolve(resolveValue);
+ }, delay);
+
+ promise.resolve = resolve;
+ promise.reject = reject;
+
+ return promise;
+}