Last active
April 6, 2021 00:44
-
-
Save gladchinda/c608027eeb43ebc89e8561b1466b260e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function multiConsumablePromiseFactory(getPromiseFn, skipWaitingFn) { | |
let _this, _promise, _controller, _signal; | |
function _reset() { | |
_this = _promise = _controller = _signal = undefined; | |
} | |
function _skipWaiting() { | |
if (_controller instanceof AbortController) { | |
typeof skipWaitingFn === 'function' && skipWaitingFn.call(_this); | |
_controller.abort(); | |
} | |
_reset(); | |
} | |
function _multiConsumablePromise() { | |
if (!_promise) { | |
_promise = new Promise((resolve, reject) => { | |
_this = this; | |
_controller = new AbortController(); | |
_signal = _controller.signal; | |
new Promise((resolve, reject) => { | |
_signal.addEventListener("abort", () => resolve()); | |
Promise.resolve(typeof getPromiseFn === 'function' ? getPromiseFn.call(_this) : getPromiseFn) | |
.then(resolve, reject /* () => resolve() */); | |
}).then(resolve, reject); | |
}).finally(_reset); | |
} | |
return _promise; | |
} | |
Object.defineProperty(_multiConsumablePromise, 'skipWaiting', { | |
get() { return _skipWaiting } | |
}); | |
return _multiConsumablePromise; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment