class ControllablePromise extends Promise {
constructor() {
let resolveClosure = null;
let rejectClosure = null;
super((resolve, reject) => {
resolveClosure = resolve;
rejectClosure = reject;
});
this.resolveClosure = resolveClosure;
this.rejectClosure = rejectClosure;
}
}
const prom = new ControllablePromise();
async function func(){
try{
await prom;
console.log('done');
}
catch(err){
console.log(err.message);
}
}
setTimeout(() => {prom.rejectClosure(); console.log('resolved');}, 1000);
func();
I am attempting to create a promise that I can resolve/reject externally. I can do this functionally without issue, just returning a regular promise after I attach the resolve/reject closures to it, but when attempting to turn it into a class I am running into difficulty. The code above throws an error when attempting to await the promise. Why is this happening exactly?