Для этого вам нужно сделать свой forEach asyn c
if (cart) {
async function asyncForEach(arr) {
const promises = arr.products.forEach(async(product) => {
const productItem = await ProductModel
.findOne({_id: product.productDetailsId });
cartProducts.push(productItem);
});
await Promise.all(promises);
}
asyncForEach(cart);
}
function mockAsync(param) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(console.log(param)), 2000)
})
}
let cart = ["A", "B", "C"];
async function asyncForEach(arr) {
console.log("processing...")
const promises = arr.forEach(async(product) => {
await mockAsync(product);
});
await Promise.all(promises)
}
asyncForEach(cart);