So I have an asynchronous function that I need to call a number of times. Naturally, I would like to use Promise.all
const promises = [];
ids.forEach((id) => {
stockoutPromises.push(
asyncFunc(id),
});
});
results[] = Promise.all(promises);
Okay no problem there but how do I know which id goes with which result? Sure I could iterate through both arrays again, but is there another way to do this? Is there a way that I can create a promise that when resolved gives me an object that contains both id and result?
Thanks!
Chain a
.then
onto the call ofasyncFunc
so that the resulting item is not just of theasyncFunc
result, but of it and the ID in an object:But it'd be better to use
.map
instead of.push
in a loop: