Created
November 29, 2018 11:19
-
-
Save rvanmil/45811315b2680100d12e9d4b6b8c59db to your computer and use it in GitHub Desktop.
Parallel and sequential async processing
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 randomTimeout = () => Math.floor(Math.random() * (3000 - 500)) + 500 | |
const someAsyncProcess = message => new Promise(resolve => setTimeout(() => { | |
console.log(message) | |
resolve() | |
}, randomTimeout())) | |
const messages = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
const testParallel = async () => Promise.all(messages.map(async message => someAsyncProcess(message))) | |
const testSequential = async () => messages.reduce(async (previousPromise, message) => { | |
await previousPromise | |
return someAsyncProcess(message) | |
}, Promise.resolve()) | |
const test = async () => { | |
console.log('Parallel run') | |
await testParallel() | |
console.log('--------------') | |
console.log('Sequantial run') | |
await testSequential() | |
} | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hoi René,
Even voor mijn duidelijkheid:
const testParallel = ASYNC () => Promise.all(messages.map(ASYNC message => someAsyncProcess(message)))
const testSequential = ASYNC () => messages.reduce(async (previousPromise, message) => {
Volgens mijn zijn de
async
-en niet nodig op regels 10 en 12 omdat jij binnen die functies geenawait
hebt gebruikt.Klopt het?