You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a continuation of my first post on building a custom MyPromise class which imitates the behavior of the standard Promise class that is used to handle asynchronous logic in javascript. In this post we will be implementing the all, resolve, finally, catch method. Recall that we ended with the following code last time:
. As we can see, the all method will take an array of variables with each of them either being a MyPromise object or not. As the name suggests, if any of the MyPromise in the array rejects, it will return a REJECTED MyPromise, otherwise a FULFILLED MyPromise. If we run the above program, the expected result is: [ 'a', 'b', 'p1', 'p2', 'c' ]. Notice that we are calling all on MyPromise directly, so it is a static method.
staticall(array){letresult=[];functionaddData(key,value){result[key]=value;}returnnewMyPromise((resolve,reject)=>{for(leti=0;i<array.length;i++){letcurrent=array[i];if(currentinstanceofMyPromise){//it it is a promise object, call its then methodcurrent.then(value=>addData(i,value),reason=>reject(reason))}else{//if it is an ordinary value, store it directlyaddData(i,array[i]);}}resolve(result);})};
The resolve method
We want to implement another static method called resolve:
, and it accepts a variable(either MyPromise or not). If it receives an ordinary value like the 10 above, it will create a MyPromise which resolves the corresponding value. If it recieves a MyPromise object, it will return that object directly.
, finally will be executed whatever the status of p2 and it will return a new MyPromise which carries the info from p2 to the next call to then. The expected output of the above program is
finally
p2 resolve
. Notice that if finally returns a MyPromise object with asynchronous process inside of it, the next then will wait for that process to terminate before it is executed.
finally(callback){returnthis.then((value)=>{//using resolve here because we want to ensure that callback is executed firstreturnMyPromise.resolve(callback()).then(()=>value);},(reason)=>{//using resolve here because we want to ensure that callback is executed firstreturnMyPromise.resolve(callback()).then(()=>{throwreason;});});}
The catch method
Finally, the catch method returns a MyPromise deals with rejected cases only:
.In conclusion, we arrive at the following code for out custom MyPromise class:
constPENDING="pending";constFULFILLED="fulfilled";constREJECTED="rejected";classMyPromise{status=PENDING;value=undefined;reason=undefined;successCallback=[];failureCallback=[];constructor(executor){//reject the errror if there is onetry{executor(this.resolve,this.reject);}catch(e){this.reject(e);}}resolve=(value)=>{if(this.status===PENDING){this.status=FULFILLED;}else{return;}this.value=value;while(this.successCallback.length){this.successCallback.shift()();}};reject=(reason)=>{if(this.status===PENDING){this.status=REJECTED;}else{return;}this.reason=reason;while(this.failureCallback.length){this.failureCallback.shift()();}};then=(successCallback,failureCallback)=>{successCallback=successCallback ? successCallback : (value)=>value;failureCallback=failureCallback
? failureCallback
: (reason)=>{throwreason;};letpromise2=newMyPromise((resolve,reject)=>{if(this.status===FULFILLED){setTimeout(()=>{try{letx=successCallback(this.value);resolvePromise(promise2,x,resolve,reject);}catch(e){reject(e);}},0);}elseif(this.status===REJECTED){setTimeout(()=>{try{letx=failureCallback(this.reason);resolvePromise(x,resolve,reject);}catch(e){reject(e);}},0);}else{this.successCallback.push(()=>{setTimeout(()=>{try{letx=successCallback(this.value);resolvePromise(promise2,x,resolve,reject);}catch(e){reject(e);}},0);});this.failureCallback.push(()=>{setTimeout(()=>{try{letx=failureCallback(this.reason);resolvePromise(promise2,x,resolve,reject);}catch(e){reject(e);}},0);});}});returnpromise2;};finally(callback){returnthis.then((value)=>{returnMyPromise.resolve(callback()).then(()=>value);},(reason)=>{returnMyPromise.resolve(callback()).then(()=>{throwreason;});});}catch(failureCallback){returnthis.then(undefined,failureCallback);}staticall(array){letresult=[];letindex=0;returnnewMyPromise((resolve,reject)=>{functionaddData(key,value){result[key]=value;index++;if(index===array.length){resolve(result);}}for(leti=0;i<array.length;i++){letcurrent=array[i];if(currentinstanceofMyPromise){current.then((value)=>addData(i,value),(reason)=>reject(reason));}else{//if it is an ordinary value, store it directlyaddData(i,array[i]);}}});}staticresolve(value){if(valueinstanceofMyPromise){returnvalue;}returnnewMyPromise((resolve)=>resolve(value));}}functionresolvePromise(promise2,x,resolve,reject){if(promise2===x){returnreject(newTypeError("Chaining cycle detected for promise #<MyPromise>"));}if(xinstanceofMyPromise){x.then(resolve,reject);}else{resolve(x);}}module.exports=MyPromise;
, and this concludes the content of this post.
The text was updated successfully, but these errors were encountered:
This is a continuation of my first post on building a custom MyPromise class which imitates the behavior of the standard Promise class that is used to handle asynchronous logic in javascript. In this post we will be implementing the all, resolve, finally, catch method. Recall that we ended with the following code last time:
The All method
We want to implement the All method that comes with the standard Promise class:
. As we can see, the all method will take an array of variables with each of them either being a MyPromise object or not. As the name suggests, if any of the MyPromise in the array rejects, it will return a REJECTED MyPromise, otherwise a FULFILLED MyPromise. If we run the above program, the expected result is:
[ 'a', 'b', 'p1', 'p2', 'c' ]
. Notice that we are calling all on MyPromise directly, so it is a static method.The resolve method
We want to implement another static method called resolve:
, and it accepts a variable(either MyPromise or not). If it receives an ordinary value like the 10 above, it will create a MyPromise which resolves the corresponding value. If it recieves a MyPromise object, it will return that object directly.
The finally method
Observe the following use case for the finally method we are going to implement
, finally will be executed whatever the status of p2 and it will return a new MyPromise which carries the info from p2 to the next call to then. The expected output of the above program is
. Notice that if finally returns a MyPromise object with asynchronous process inside of it, the next then will wait for that process to terminate before it is executed.
The catch method
Finally, the
catch
method returns a MyPromise deals with rejected cases only:and the expected output is:
. The implementation is straightforward:
.In conclusion, we arrive at the following code for out custom MyPromise class:
, and this concludes the content of this post.
The text was updated successfully, but these errors were encountered: