Skip to content

Instantly share code, notes, and snippets.

@ashwinkumar2438
Last active February 21, 2022 03:52
Show Gist options
  • Select an option

  • Save ashwinkumar2438/f0cda4a8858a48f9218ef7675965735a to your computer and use it in GitHub Desktop.

Select an option

Save ashwinkumar2438/f0cda4a8858a48f9218ef7675965735a to your computer and use it in GitHub Desktop.

Revisions

  1. ashwinkumar2438 revised this gist Feb 21, 2022. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion resolve.js
    Original file line number Diff line number Diff line change
    @@ -7,12 +7,14 @@ class PromiseCopy{
    */
    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 = State.fulfilled ;
    this.#state = states.fulfilled ;
    this.#result = value ;

    this.#dispatchCallbacks();
  2. ashwinkumar2438 revised this gist Feb 21, 2022. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion resolve.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,6 @@ class PromiseCopy{


    //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 ) );


  3. ashwinkumar2438 created this gist Feb 21, 2022.
    25 changes: 25 additions & 0 deletions resolve.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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 ) ) ;


    //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 = State.fulfilled ;
    this.#result = value ;

    this.#dispatchCallbacks();

    }

    #dispatchCallbacks(){ /* */ } ;

    }