Undead8
Undead8

Reputation: 202

Asynchronous call over array in javascript (Promise.all)

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!

Upvotes: 0

Views: 868

Answers (1)

Bergi
Bergi

Reputation: 664444

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:

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.

It's not a bad way. You are using the returned array: you're passing it to Promise.all. It doesn't work without that. obj.build() returns a promise, and you need to collect those into an array. map is the perfect solution for that.

It would be even better if the promises don't fulfill with undefined (i.e. have no result), but with the built object, so that you would write

async function buildFrom(oldObj) {
    …
    return newObj;
}
const builtObjects = await Promise.all(myObjArray.map(buildFrom));

Upvotes: 1

Related Questions