Skip to content

Instantly share code, notes, and snippets.

@ashwinkumar2438
Last active February 19, 2022 20:51
Show Gist options
  • Save ashwinkumar2438/5375d27db0fffef5bdd6ec9470ca8019 to your computer and use it in GitHub Desktop.
Save ashwinkumar2438/5375d27db0fffef5bdd6ec9470ca8019 to your computer and use it in GitHub Desktop.
const error = null ;
//const error = 'error' ; // change to a truthy value to reject promise.
const promise = new Promise( ( resolve, reject ) => {
//asynchronous operation
setTimeout( () => {
return error ? reject( error ) : resolve( 'final data' ) ;
}, 3000 );
} );
// before 3 seconds:
Promise {<pending>}
[[PromiseState]]: "pending"
[[PromiseResult]]: undefined
//resolved after 3 seconds :
Promise {<fulfilled>: 'final data'}
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: "final data"
//rejected after 3 seconds :
Promise {<rejected>: 'error'}
[[PromiseState]]: "rejected"
[[PromiseResult]]: "error"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment