Last active
January 7, 2016 18:43
-
-
Save StevenACoffman/f2b0948168189821a5d0 to your computer and use it in GitHub Desktop.
Promise example
This file contains 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
'use strict'; | |
const start = Date.now(); | |
const ordinals = { | |
1: 'First', | |
2: 'Second', | |
3: 'Third', | |
4: 'Fourth', | |
5: 'Fifth' | |
}; | |
const whatIsHappening = (result) => console.log(`${ordinals[result]} @ ${Date.now() - start} ms`); | |
new Promise(resolve => resolve(4)) | |
.then(result => { | |
whatIsHappening(result); | |
return new Promise(resolve => resolve(result)); | |
}) | |
.then(() => whatIsHappening(5)); | |
whatIsHappening(1); | |
whatIsHappening(2); | |
whatIsHappening(3); | |
new Promise((resolve, reject) => | |
reject(new Error('failed to deliver on my promise to you'))) | |
.catch(reason => console.log(reason)); | |
// <- Error: failed to deliver on my promise to you |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment