| Index: polyfill.js |
| =================================================================== |
| --- a/polyfill.js |
| +++ b/polyfill.js |
| @@ -93,40 +93,46 @@ |
| // If the last argument is undefined, we drop it from the list assuming |
| // it stands for the optional callback. We must do this, because we have |
| // to replace it with our own callback. If we simply append our own |
| // callback to the list, it won't match the signature of the function and |
| // will cause an exception. |
| if (typeof args[args.length - 1] == "undefined") |
| args.pop(); |
| + let resolvePromise = null; |
| + let rejectPromise = null; |
| + |
| + func.call(object, ...args, result => |
| + { |
| + let error = browser.runtime.lastError; |
| + if (error && !portClosedBeforeResponseError.test(error.message)) |
| + { |
| + // runtime.lastError is already an Error instance on Edge, while on |
| + // Chrome it is a plain object with only a message property. |
| + if (!(error instanceof Error)) |
| + { |
| + error = new Error(error.message); |
| + |
| + // Add a more helpful stack trace. |
| + error.stack = callStack; |
| + } |
| + |
| + rejectPromise(error); |
| + } |
| + else |
| + { |
| + resolvePromise(result); |
|
kzar
2018/02/06 09:49:24
I wonder if it could be possible that resolvePromi
Manish Jethani
2018/02/06 11:51:37
Since the APIs we are wrapping are asynchronous, t
|
| + } |
| + }); |
| + |
| return new Promise((resolve, reject) => |
| { |
| - func.call(object, ...args, result => |
| - { |
| - let error = browser.runtime.lastError; |
| - if (error && !portClosedBeforeResponseError.test(error.message)) |
| - { |
| - // runtime.lastError is already an Error instance on Edge, while on |
| - // Chrome it is a plain object with only a message property. |
| - if (!(error instanceof Error)) |
| - { |
| - error = new Error(error.message); |
| - |
| - // Add a more helpful stack trace. |
| - error.stack = callStack; |
| - } |
| - |
| - reject(error); |
| - } |
| - else |
| - { |
| - resolve(result); |
| - } |
| - }); |
| + resolvePromise = resolve; |
| + rejectPromise = reject; |
| }); |
| }; |
| Object.defineProperty(object, name, descriptor); |
| } |
| function wrapRuntimeOnMessage() |
| { |