I am wondering what is the best practice when we need to call an asynchronous function on each elements of an array and wait for all the Promises to resolve. Assume that we do not need to change the original array.
For instance, let's say that I have an array of objects that each need to be built asynchronously with a build() function, which return a Promise. I need to wait for them to be built before moving on with following instructions. Currently, I will do the following.
async function myFunction() {
await Promise.all(myObjArray.map(myObj => myObj.build()));
{...}
}
However, I think that this may be a bad way to do it because the Array.prototype.map() function should not be used in that case, as per the documentation. However, I do not know what would be a good alternative since forEach would not work.
When not to use map()
Since map builds a new array, using it when you aren't using the returned array is an anti-pattern; use forEach or for...of instead.
You shouldn't be using map if:
you're not using the array it returns; and/or you're not returning a value from the callback.
Thanks!
.build()operations in parallel or in sequence? Your proposed use of.map()runs them all in parallel.