Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence of await functions call

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(); 
}
like image 780
Brainfail Avatar asked Sep 08 '25 00:09

Brainfail


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()));
like image 94
JLRishe Avatar answered Sep 09 '25 13:09

JLRishe