Skip to content

Instantly share code, notes, and snippets.

@martin-sweeny
Created January 13, 2018 14:01
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 martin-sweeny/b4fdb0ffe692c778438c6947b0c1386f to your computer and use it in GitHub Desktop.
Save martin-sweeny/b4fdb0ffe692c778438c6947b0c1386f to your computer and use it in GitHub Desktop.
Playing with async/await
timed = async i => new Promise(resolve => {
console.log(`starting ${i}`)
setTimeout(() => {
resolve('done!');
console.log('yo', i)
}, Math.random() * 5000)
});
parallel = async count => {
timers = [], msgs = [], indeces = [], start = count;
while (--count > 0) {
indeces.push(start - count);
timers.push(timed);
}
indeces = indeces.reverse()
console.log('indeces', indeces.reverse())
const promises = timers.map((timed, i) => timed(indeces[i]))
Promise.all(promises).then(val => console.log(val))
}
parallel(5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment