What is the correct fix?
async function loadAll(urls: string[]) {
const out = [];
urls.forEach(async (u) => {
out.push(await fetch(u));
});
return out;
}
- Remove
asyncfrom the callback - Change
forEachtomaponly - Add
awaitbeforeforEach - Use
await Promise.all(urls.map(u => fetch(u)))
forEach does not await; collect promises with Promise.all.