In this code block:
if I declare
resolve / rejectwithconstkeyword,I need to pass them into
executorfunction withoutthis, and usingthiswill get error;if I declare
resolve / rejectwiththiskeyword,I need to pass them into
executorfunction withthis.
What cause this different behavior?
And what differences between these two ways?
Please help me, thanks~
class myPromise {
constructor(executor) {
this.status = PENDING;
this.value = null;
this.reason = null;
const resolve = value => {
this.value = value;
}
this.reject = reason => {
this.reason = reason;
}
// Why not use this.resolve / this.reject ?
//
executor(resolve, this.reject);
}
then(onfulfilled = Function.prototype, onrejected = Function.prototype) {
onfulfilled(this.value);
onrejected(this.reason);
}
}