Skip to content

Instantly share code, notes, and snippets.

@FFKL
Created August 22, 2020 09:28
Show Gist options
  • Save FFKL/5af4e59428cac4a539650f3981964f1d to your computer and use it in GitHub Desktop.
Save FFKL/5af4e59428cac4a539650f3981964f1d to your computer and use it in GitHub Desktop.
class Cancelable extends Promise {
// Symbol.species specifies a function-valued property that the constructor function uses to create derived objects.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/species
static get [Symbol.species]() {
return Promise;
}
constructor(executor) {
let _reject;
super((resolve, reject) => {
executor(resolve, reject);
_reject = reject;
});
this._reject = _reject;
this.canceled = false;
}
cancel() {
this.canceled = true;
this._reject(new Error('Canceled'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment