Skip to content

Instantly share code, notes, and snippets.

@ashwinkumar2438
Last active February 24, 2022 18:14
Show Gist options
  • Save ashwinkumar2438/a4c5b3b473598c97667cdc94dc4ab880 to your computer and use it in GitHub Desktop.
Save ashwinkumar2438/a4c5b3b473598c97667cdc94dc4ab880 to your computer and use it in GitHub Desktop.
class PromiseCopy{
#handlers = [] ;
#queued = false ;
#dispatchCallbacks(){
if( this.#state === states.pending )return ;
if( this.#queued )return ;
const method = this.#state === states.fulfilled ? 'success' : 'fail' ; //handler method to call
queueMicrotask( () => {
//log promise error if promise is rejected and no handlers are available.
if( this.#state === states.rejected && !this.#handlers.length ) console.error( 'Uncaught (in promisecopy)', this.#result );
for( let handler of this.#handlers ){
handler[ method ]( this.#result ) ;
}
//cleanup activities:
this.#handlers = []; //remove triggered handlers.
this.#queued = false ;
} );
this.#queued = true ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment