Created
January 13, 2018 14:01
-
-
Save martin-sweeny/b4fdb0ffe692c778438c6947b0c1386f to your computer and use it in GitHub Desktop.
Playing with async/await
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
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