Skip to content

Instantly share code, notes, and snippets.

@daronwolff
Created June 16, 2017 20:18
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 daronwolff/d13995847bfc49ae2ad0fc8f360fe6e5 to your computer and use it in GitHub Desktop.
Save daronwolff/d13995847bfc49ae2ad0fc8f360fe6e5 to your computer and use it in GitHub Desktop.
Executing promises sequentially
const times = [ 1, 3, 4, 2 ];
const sleep = ms =>
new Promise(res => {
const t = ms * 1000;
setTimeout(res, t)
})
const myPromise = num =>
sleep(num).then(() => {
console.log('done: ' + num)
})
times.reduce(
(p, x) =>
p.then(_ => myPromise(x)),
Promise.resolve()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment