Skip to content

Instantly share code, notes, and snippets.

@maxfarseer
Last active April 27, 2016 09:00
Show Gist options
  • Save maxfarseer/88458bd2284fb6b31459a605b80d6ba9 to your computer and use it in GitHub Desktop.
Save maxfarseer/88458bd2284fb6b31459a605b80d6ba9 to your computer and use it in GitHub Desktop.
// won't work correctly
let counter = 0;
function delay(ms) {
return new Promise((resolve, reject) => {
setTimeout(function() {
console.log(++counter)
resolve()
}, ms);
});
}
delay(1000)
.then(delay(1000))
.then(delay(2000))
.then(() => console.log("Hello!"))
// ---
// work correctly
let counter = 0;
function delay(ms) {
return new Promise((resolve, reject) => {
setTimeout(function() {
console.log(++counter)
resolve()
}, ms);
});
}
delay(1000)
.then(() => delay(1000))
.then(() => delay(2000))
.then(() => console.log("Hello!"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment