async function init() {
const promises = myObjects.map(async myObject => {
return myFunction(myObject);
});
const result = await Promise.all(promises);
}
async function myFunction(myObject) {
let arrayA = [];
functionBresponse = await functionB(); //Basic utility function to fetch some items
arrayA = arrayA.concat(functionBresponse);
const res = {"Completed": 200};
return res;
}
I am using promise.all to ensure myFunction runs async for the myObjects array. The problem here is that the local variable arrayA gets overwritten by the most recent iteration. I have looked into closures and understand the basic concept. However, I am unsure how to use it here.