$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #42

What is the correct fix?

async function loadAll(urls: string[]) {
  const out = [];
  urls.forEach(async (u) => {
    out.push(await fetch(u));
  });
  return out;
}
  1. Remove async from the callback
  2. Change forEach to map only
  3. Add await before forEach
  4. Use await Promise.all(urls.map(u => fetch(u)))
forEach does not await; collect promises with Promise.all.
Play Problem Solving ▸