Last active
March 31, 2016 17:17
-
-
Save andyedinborough/61d4982a355421d2585b to your computer and use it in GitHub Desktop.
Revisions
-
andyedinborough revised this gist
Mar 31, 2016 . 1 changed file with 16 additions and 52 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,67 +1,31 @@ const noop = () => {}; export default class CancellablePromise { constructor(executorOrPromise){ this._promise = new Promise((resolve, reject) => { let promise = executorOrPromise; if(!promise.then) promise = new Promise(executorOrPromise); promise.then((...args) => { if(this.cancelled) reject('cancelled', ...args); else resolve(...args); }); promise.catch(reject); }); } cancel() { this.cancelled = true; this.catch(noop); return this; } then(func) { return new CancellablePromise(this._promise.then(func)); } catch(func) { return new CancellablePromise(this._promise.catch(func)); } } -
andyedinborough revised this gist
Dec 20, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ class CancellablePromise { this.cancelled = false; this._resolved = false; this._caught = false; this._thens = []; this._catches = []; this._queue = this._thens; -
andyedinborough created this gist
Dec 20, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,67 @@ class CancellablePromise { constructor(executorOrPromise) { let promise = executorOrPromise; if (!promise.then) promise = new Promise(executorOrPromise); this.cancelled = false; this._resolved = false; this._caught = true; this._thens = []; this._catches = []; this._queue = this._thens; this._result = null; this._process = result => { this._resolved = true; const then = this._queue.shift(); if (then) { try { this._result = then(result); } catch (x) { _catch(x); return; } if (this._result && this._result.then) { this._result = new CancellablePromise(this._result) .then(this._process) .catch(this._process); } else this._process(this._result); } }; const _then = result => this._process(result); const _catch = result => { this._caught = true; this._queue = this._catches; this._process(result); }; promise .then(_then) .catch(_catch); } cancel() { this.cancelled = true; this._queue = this._catches; this._resolved = true; this._caught = true; this._process({ cancelled: true }); } then(func) { this._thens.push(func); if (this._resolved && !this._caught) { this._process(this._result); } return this; } catch(func) { this._catches.push(func); if (this._resolved && this._caught) { this._process(this._result); } return this; } }