Last active
February 21, 2022 03:52
-
-
Save ashwinkumar2438/f0cda4a8858a48f9218ef7675965735a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PromiseCopy{ | |
static resolve( value ){ | |
/* | |
1. We return a new Promise and pass the value to resolve method if Promise.resolve is called directly. | |
2. The resolve method of executor runs this same code with this instance added. | |
*/ | |
if( !( this instanceof PromiseCopy ) ) return new PromiseCopy( res => res( value ) ) ; | |
//return if promise is settled. | |
if( this.#state !== states.pending )return ; | |
//If value is a thenable object we call Promise.resolve recursively by passing it to value.then. | |
if( isThenable( value ) ) return value.then( PromiseCopy.resolve.bind( this ), PromiseCopy.resolve.bind( this ) ); | |
this.#state = states.fulfilled ; | |
this.#result = value ; | |
this.#dispatchCallbacks(); | |
} | |
#dispatchCallbacks(){ /* */ } ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment