Skip to content

Instantly share code, notes, and snippets.

@ashwinkumar2438
Last active February 26, 2022 12:02
Show Gist options
  • Save ashwinkumar2438/1681801ccc70222c91bb60e147e892f8 to your computer and use it in GitHub Desktop.
Save ashwinkumar2438/1681801ccc70222c91bb60e147e892f8 to your computer and use it in GitHub Desktop.
class PromiseCopy{
finally( callback ){
// resolveOrReject : forward current #state.
// value : forward current #result.
/** curried function to pass Promise.resolve or
Promise.reject forward based on current Promise #state. **/
const commonCallback = ( resolveOrReject ) => ( value ) => {
const response = callback?.() ;
return Promise.resolve( response ).then( _ => resolveOrReject( value ) );
}
return this.then( commonCallback( Promise.resolve ) , commonCallback( Promise.reject ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment