Last active
June 25, 2017 07:57
-
-
Save tbranyen/b4e626cf2e2bd3cccac0ae3b93339808 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
class AbortablePromise extends Promise { | |
constructor(fn) { | |
let _reject = null; | |
super((resolve, reject) => { | |
_reject = reject; | |
return fn(resolve, reject); | |
}); | |
this.abort = () => { | |
this.abort.aborted = true; | |
_reject(null); | |
return this; | |
} | |
} | |
then(resolve, reject) { | |
const promise = super.then(val => { | |
if (this.abort.aborted && reject) { | |
return reject(null); | |
} | |
else if (!this.abort.aborted && resolve) { | |
return resolve(val); | |
} | |
}, () => {}); | |
promise.abort = this.abort; | |
return promise; | |
} | |
resolve(promise) { | |
return new AbortablePromise((res, rej) => promise.then(res).catch(rej)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment