Skip to content

Instantly share code, notes, and snippets.

@arosh
Last active March 14, 2017 02:37
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 arosh/d06e56ac5e9f7817ab89319570977252 to your computer and use it in GitHub Desktop.
Save arosh/d06e56ac5e9f7817ab89319570977252 to your computer and use it in GitHub Desktop.
Promise sleep
function wait () {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000)
})
}
function foo (i) {
return new Promise((resolve, reject) => {
console.log(i)
resolve()
})
}
let promise = Promise.resolve()
for (let i = 1; i <= 3; i++) {
if (i >= 2) {
promise = promise.then(() => wait())
}
promise = promise.then(() => foo(i))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment