I want to add a cancel
method to a subclass of the Promise
built-in. Why does this not work?
class CancellablePromise extends Promise {
constructor(executor) {
let cancel = null
super((resolve,reject) => {
cancel = reject
executor(resolve, reject)
})
this.cancel = cancel
}
}
const p = new CancellablePromise((resolve) => setTimeout(resolve, 1000))
.then(() => console.log('success'))
.catch((err) => console.log('rejected', err))
p.cancel() // Uncaught exception
Is the answer todo with Symbol.species
?
Promise
class. – Carrissa