summaryrefslogtreecommitdiff
path: root/ext/mixed
diff options
context:
space:
mode:
authorsiikamiika <siikamiika@users.noreply.github.com>2020-04-04 17:02:54 +0300
committersiikamiika <siikamiika@users.noreply.github.com>2020-04-05 19:57:37 +0300
commit867902523f43929393500574076f793c4f62c225 (patch)
tree5b8995601acacc20f09f14849d11c0724ebaeeb5 /ext/mixed
parent123e725049e5c6b5c3b7b53ee37aaf93e7bb0c61 (diff)
simplify promise
Diffstat (limited to 'ext/mixed')
-rw-r--r--ext/mixed/js/core.js51
1 files changed, 22 insertions, 29 deletions
diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js
index 7fb3c32e..5f9e172d 100644
--- a/ext/mixed/js/core.js
+++ b/ext/mixed/js/core.js
@@ -318,13 +318,6 @@ const yomichan = (() => {
}
getTemporaryListenerResult(eventHandler, userCallback, timeout=null) {
- let resolve;
- let reject;
- const listenerPromise = new Promise((_resolve, _reject) => {
- resolve = _resolve;
- reject = _reject;
- });
-
if (
typeof eventHandler.addListener === 'undefined' ||
typeof eventHandler.removeListener === 'undefined'
@@ -332,32 +325,32 @@ const yomichan = (() => {
throw new Error('Event handler type not supported');
}
- const runtimeMessageCallback = ({action, params}, sender, sendResponse) => {
- let timeoutId = null;
- if (timeout !== null) {
- timeoutId = window.setTimeout(() => {
- timeoutId = null;
- eventHandler.removeListener(runtimeMessageCallback);
- reject(new Error(`Listener timed out in ${timeout} ms`));
- }, timeout);
- }
-
- const cleanupResolve = (value) => {
- if (timeoutId !== null) {
- window.clearTimeout(timeoutId);
- timeoutId = null;
+ return new Promise((resolve, reject) => {
+ const runtimeMessageCallback = ({action, params}, sender, sendResponse) => {
+ let timeoutId = null;
+ if (timeout !== null) {
+ timeoutId = window.setTimeout(() => {
+ timeoutId = null;
+ eventHandler.removeListener(runtimeMessageCallback);
+ reject(new Error(`Listener timed out in ${timeout} ms`));
+ }, timeout);
}
- eventHandler.removeListener(runtimeMessageCallback);
- sendResponse();
- resolve(value);
- };
- userCallback({action, params}, {resolve: cleanupResolve, sender});
- };
+ const cleanupResolve = (value) => {
+ if (timeoutId !== null) {
+ window.clearTimeout(timeoutId);
+ timeoutId = null;
+ }
+ eventHandler.removeListener(runtimeMessageCallback);
+ sendResponse();
+ resolve(value);
+ };
- eventHandler.addListener(runtimeMessageCallback);
+ userCallback({action, params}, {resolve: cleanupResolve, sender});
+ };
- return listenerPromise;
+ eventHandler.addListener(runtimeMessageCallback);
+ });
}
// Private