Sequence of await functions call

Viewed 496

i'd like to know how can i beautify a code snippet in javascript, which Looks like that

async sequenceOfFunctionCalls () {
   await callFunction1();
   await callFunction2();
   ...
   await callFunctionN(); 
}
1 Answers

Assuming you want to run them in sequence (not in parallel), I'd say the simplest option is:

for (let func of [callFunction1, callFunction2, ..., callFunctionN]) {
    await func();
}

To run them in parallel:

await Promise.all([callFunction1, callFunction2, ..., callFunctionN].map(f => f()));

or

await Promise.allSettled([callFunction1, callFunction2, ..., callFunctionN].map(f => f()));
Related