Created
October 3, 2018 10:24
-
-
Save evanderkoogh/eb8152ce6bb2c0c281c947d10b7a972c to your computer and use it in GitHub Desktop.
How to execute promises both serially and in parallel
This file contains 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
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