Skip to content

Instantly share code, notes, and snippets.

@tbranyen
Last active June 25, 2017 07:57
Show Gist options
  • Save tbranyen/b4e626cf2e2bd3cccac0ae3b93339808 to your computer and use it in GitHub Desktop.
Save tbranyen/b4e626cf2e2bd3cccac0ae3b93339808 to your computer and use it in GitHub Desktop.
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