Skip to content

Instantly share code, notes, and snippets.

@StevenACoffman
Last active January 7, 2016 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenACoffman/f2b0948168189821a5d0 to your computer and use it in GitHub Desktop.
Save StevenACoffman/f2b0948168189821a5d0 to your computer and use it in GitHub Desktop.
Promise example
'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