Created
August 22, 2020 09:28
-
-
Save FFKL/5af4e59428cac4a539650f3981964f1d to your computer and use it in GitHub Desktop.
This file contains 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 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