Last active
August 29, 2015 14:10
-
-
Save briancavalier/c416d396e844e31c6efa 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 AbortablePromise(resolver) { | |
| if (typeof resolver !== 'function') | |
| throw new Error('AbortablePromise needs a resolver function'); | |
| var abort; | |
| var promise = new Promise(function (resolve, reject) { | |
| var aborter; | |
| abort = function () { | |
| if (aborter == null) | |
| return; | |
| var fn = aborter; | |
| aborter = null; | |
| try { | |
| return fn.apply(this, arguments); | |
| } catch (error) { | |
| reject(error); | |
| } | |
| }; | |
| // Resolver should return an aborter | |
| aborter = resolver(function (child) { | |
| if (child && typeof child.abort === 'function') { | |
| aborter = child.abort; | |
| } else { | |
| aborter = null; | |
| } | |
| resolve.apply(this, arguments); | |
| }, function () { | |
| aborter = null; | |
| reject.apply(this, arguments); | |
| }); | |
| if(typeof aborter !== 'function') { | |
| // Make rejecting be the default abort behavior. | |
| // Not sure if this is good or bad, but maybe worth discussion | |
| aborter = reject; | |
| } | |
| }); | |
| return makeAbortable(promise, abort); | |
| } |
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 AbortablePromise(resolver) { | |
| if (typeof resolver !== 'function') | |
| throw new Error('AbortablePromise needs a resolver function'); | |
| var abort; | |
| var promise = new Promise(function (resolve, reject) { | |
| var aborter; | |
| abort = function () { | |
| if (aborter == null) | |
| return; | |
| var fn = aborter; | |
| aborter = null; | |
| var abortResult; | |
| try { | |
| // aborter may return a promise to control the state of | |
| // the promise, rather than having to capture the | |
| // resolve/reject functions | |
| abortResult = fn.apply(this, arguments); | |
| resolve(abortResult); | |
| return abortResult; | |
| } catch (error) { | |
| reject(error); | |
| } | |
| }; | |
| // Resolver should return an aborter | |
| aborter = resolver(function (child) { | |
| if (child && typeof child.abort === 'function') { | |
| aborter = child.abort; | |
| } else { | |
| aborter = null; | |
| } | |
| resolve.apply(this, arguments); | |
| }, function () { | |
| aborter = null; | |
| reject.apply(this, arguments); | |
| }); | |
| if(typeof aborter !== 'function') { | |
| // Make rejecting be the default abort behavior. | |
| // Not sure if this is good or bad, but maybe worth discussion | |
| aborter = reject; | |
| } | |
| }); | |
| return makeAbortable(promise, abort); | |
| } |
mjackson
commented
Dec 8, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment