Skip to content

Instantly share code, notes, and snippets.

@ashwinkumar2438
Last active February 21, 2022 03:52
Show Gist options
  • Save ashwinkumar2438/f0cda4a8858a48f9218ef7675965735a to your computer and use it in GitHub Desktop.
Save ashwinkumar2438/f0cda4a8858a48f9218ef7675965735a to your computer and use it in GitHub Desktop.
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