Last active
February 24, 2022 18:14
-
-
Save ashwinkumar2438/a4c5b3b473598c97667cdc94dc4ab880 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{ | |
#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