As far as I understand, in ES7/ES2016 putting multiple await's in code will work similar to chaining .then() with promises, meaning that they will execute one after the other rather than in parallerl. So, for example, we have this code:
await someCall(); await anotherCall();
Do I understand it correctly that anotherCall() will be called only when someCall() is completed? What is the most elegant way of calling them in parallel?
I want to use it in Node, so maybe there's a solution with async library?
Combine the result into one line code (plus, concat, or any other function you like) could also do such trick:
const someResult = someCall();const anotherResult = anotherCall();const finalResult =[await someResult, await anotherResult]//later you can use the result with variable name.
Be aware of the moment you call functions, it may cause unexpected result:
// Supposing anotherCall() will trigger a request to create a new Userif(callFirst){ await someCall();}else{ await Promise.all([someCall(), anotherCall()]);// --> create new User here}
But following always triggers request to create new User
// Supposing anotherCall() will trigger a request to create a new Userconst someResult = someCall();const anotherResult = anotherCall();// ->> This always creates new Userif(callFirst){ await someCall();}else{const finalResult =[await someResult, await anotherResult]}