Skip to content

Instantly share code, notes, and snippets.

@wongni
Created January 12, 2018 15:19
Show Gist options
  • Save wongni/f6aa4df00082d317c40ba457b0c767c3 to your computer and use it in GitHub Desktop.
Save wongni/f6aa4df00082d317c40ba457b0c767c3 to your computer and use it in GitHub Desktop.
function printLater (number) {
return new Promise( // return a new Promise
(resolve, reject) => { // Take resolve and reject as parameters
setTimeout(() => {
if (number > 5) {
// reject causes an error
return reject('number is greater than 5')
}
resolve(number + 1) // return number + 1
console.log(number)
}, 1000)
}
)
}
printLater(1).
then(num => printLater(num)).
then(num => printLater(num)).
then(num => printLater(num)).
then(num => printLater(num)).
then(num => printLater(num)).
catch(e => console.log(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment