I am trying to execute a set of promises A,B,C,D inside a forEach. These promises should be executed sequentially for all the list items, in parallel. I expect the execution to wait until all the promises are resolved. But as soon the first promise inside the forEach is encountered, the flow starts processing the other functions in parallel. In the code snippet below, after all promises inside the list are resolved, then thirdCall should be triggered. However, it still doesn't work
async function getList(value) {
return someList;
}
async function processList(list) {
try {
await list.forEach(async (element) => {
await A(element.firstProperty); // console jumps to thirdCall() while executing A,B,C,D in parallel
await B(element.secondProperty);
await C(element.thirdProperty);
await D(element.fourthProperty);
});
return response;
} catch (e) {
throw (e);
}
}
async function thirdCall() {
try {
let someresult = someProcessing(); // this should start execution ONLY AFTER processList() is done.
await E(someresult);
thirdCallResponse;
} catch (error) {
console.log(`error deleting policy ${error}`);
throw error;
}
}
async function processValue(value) {
try {
const list = await getList(value);
await processList(list);
await thirdCall(value);
} catch (error) {
console.log(error);
}
}
processValue('someGuid');