This question concerns the Firestore database, but, more generally, it concerns making async requests in parallel.
Simply put, I wish to update multiple Firestore documents as quickly and efficiently as possible by mapping over an array of their document IDs.
the .set()
method is async (returning a promise) and so I understand that I can wrap the multiple requests - i.e. the map()
- in a Promise.all()
function, which will return a single promise once all of the requests have resolved.
However, it is not at all clear to me whether I should await the set()
within the map()
.
Whichever way I write the code (i.e. with or without await), it does not appear to make a difference to speed, but does it, or should it?
What is the correct way to achieve the greatest speed and efficiency in this instance?
const update_promises = array_of_ids.map(async id => {
const new_values = {
new_values_1: "val1",
last_update: new Date()
}
return await db.collection("my_collection").doc(id).set(new_values, { merge: true });
// OR SHOULD IT BE:
// return db.collection("my_collection").doc(id).set(new_values, { merge: true });
})
return await Promise.all(update_promises)