Last active
February 24, 2022 20:04
-
-
Save ashwinkumar2438/5585741d7dd0011e5c7377670d1c67bf 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{ | |
then( successcallback , failurecallback ){ | |
return new PromiseCopy( ( resolve, reject ) => { | |
/** | |
1. handlers is an Array which will store success handlers and error handlers. | |
2. Each entry will have : | |
- success method called with resolved value. | |
- failure method called with rejected value. | |
*/ | |
this.#handlers.push( { | |
success( value ){ | |
if( !successcallback )return resolve( value ); | |
try{ | |
resolve( successcallback( value ) ); | |
} | |
catch( e ){ | |
reject( e ) | |
} | |
}, | |
fail( error ){ | |
if( !failurecallback )return reject( error ); | |
try{ | |
resolve( failurecallback( error ) ); | |
} | |
catch( e ){ | |
reject( e ) | |
} | |
} | |
} ); | |
// execute handlers if promise is already settled. | |
this.#dispatchCallbacks(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment