Last active
March 14, 2017 02:37
-
-
Save arosh/d06e56ac5e9f7817ab89319570977252 to your computer and use it in GitHub Desktop.
Promise sleep
This file contains hidden or 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
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