Custom promise implementation(Promise polyfill)
Hi Everyone,
My name is sai and i work in Front-end stack.
Let's understand how to create your own version of promise.
Let's dive into the methods and fields present inside a promise class
This is the code sandbox link to full implementation of custom promise.
First Let's understand the flow of a promise
const promiseCheck = new MyPromise((resolve,reject)=>{
setTimeout(()=>res('Hello'),2000);
});
promiseCheck.then(response=>console.log(response)).catch(err=>err);
promiseCheck.then(response=>console.log(response + 'world');
State
The first field is state which defines what state a promise is in.
const STATE = {
FULFILLED: "fulfilled",
REJECTED: "rejected",
PENDING: "pending",
}
class MyPromise {
state = STATE.PENDING; // init state
}
Private Fields
#value; // value to store the result of the promise
#successCallbacks = []; // this is used to store the then functions
#failedCallbacks = []; // this is used to store all the catch blocks
# indicate private members, check this link for more details
Constructor
constructor(cb){
cb(this.#onResolve, this.#onReject);
}
// cb is the callback FN which will be called in promise constructor
// Example
// new MyPromise((resolve, reject) => resolve('Done')).then();
// (resolve, reject) => resolve('Done') this part is the cb
#onResolve
#onResolve = (value) => {
queueMicrotask(() => {
if (value instanceof MyPromise || value instanceof Promise){
value.then(this.#onResolve, this.#onReject);
return;
}
this.#value = value;
this.state = STATE.FULFILLED;
this.#runCallbacks();
})
}
领英推荐
Most important .then, .catch, .finally and chaining
Let's see .then
then = (resolveCb, rejectCb) => {
return new MyPromise((resolve, reject) => {
this.#successCallbacks.push((result) => {
if (resolveCb === undefined) {
resolve(result);
return;
}
try {
resolve(resolveCb(result));
} catch (err) {
reject(err);
}
});
this.#failedCallbacks.push((reject) => {
if (rejectCb === undefined) {
reject(result);
return;
}
try {
resolve(rejectCb(result));
} catch (err) {
reject(err);
}
});
this.#runCallbacks();
})
}
Catch and finally are very simple they are dependent on .then
catch = (rejectCb) => {
return this.then(undefined, rejectCb);
}
finally = (cb) => {
return this.then((value) => {
cb(value);
return value;
}, () => {
cb(value);
return value;
})
}
Finally on callbacks are being run
#runCallbacks = () => {
if (this.state === STATE.PENDING) {
return;
}
if (this.state === STATE.FULFILLED) {
this.#successCallbacks.forEach(cb => {
cb(this.#value);
})
}
if (this.state === STATE.REJECTED) {
this.#failedCallbacks.forEach(cb => {
cb(this.#value);
})
}
}
Call all the callback array based on the status.
And i referred the below links to write this article
Thanks everyone please check the below code sandbox link to check the custom promise execution
#frontend #frontendDevelopment #promise #poyfill
#webDevelopment #interview questions #interview tips #DevelopmentTips #UI #javascript #react #frontend interview
Frontend Engineer at Salesforce
1 年https://codesandbox.io/s/jovial-benji-qggtjn?file=/src/index.js