Skip to content

Instantly share code, notes, and snippets.

@evanderkoogh
Created October 3, 2018 10:24
Show Gist options
  • Save evanderkoogh/eb8152ce6bb2c0c281c947d10b7a972c to your computer and use it in GitHub Desktop.
Save evanderkoogh/eb8152ce6bb2c0c281c947d10b7a972c to your computer and use it in GitHub Desktop.
How to execute promises both serially and in parallel
const wait = async (time) => {
console.log(`Going to be waiting for ${time} seconds`)
return new Promise((resolve) => {
setTimeout(() => {
console.log(`Done waiting for ${time} seconds!`)
resolve()
}, time * 1000)
})
}
const testsWaits = async () => {
await wait(2)
await wait(3)
}
const testParallel = async () => {
const promises = [wait(2), wait(3)]
await Promise.all(promises)
}
const testSerial = async () => {
const times = [2, 3]
for (time of times) {
await wait(time)
}
}
const testForEach = async () => {
const times = [2, 3]
times.forEach(async (time) => {
await wait(time)
})
}
//testsWaits()
//testParallel()
//testSerial()
testForEach()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment