Awaiting Inside forEach
A common async/await mistake where forEach fails to wait for asynchronous work to finish.
Codejavascript
async function processOrders(ids) {
const results = [];
ids.forEach(async (id) => {
const data = await fetchOrder(id);
results.push(data);
});
console.log('Done:', results.length);
return results;
}
processOrders([1, 2, 3]);Why does this function log 'Done: 0' and return an empty array?