Bird
0
0

Identify the error in this async batch processing code snippet:

medium📝 Debug Q14 of 15
Rest API - Batch and Bulk Operations
Identify the error in this async batch processing code snippet:
async function batchFetch(urls) {
  const results = [];
  urls.forEach(async url => {
    const res = await fetch(url);
    results.push(await res.json());
  });
  return results;
}
AUsing forEach with async does not wait for all fetches to finish.
BMissing JSON.stringify on fetch body.
Cresults array is declared inside the loop.
Dfetch should not be awaited inside async function.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze async inside forEach

    forEach does not wait for async callbacks, so results may be empty on return.
  2. Step 2: Identify correct fix

    Use for...of loop with await or Promise.all to wait for all fetches.
  3. Final Answer:

    Using forEach with async does not wait for all fetches to finish. -> Option A
  4. Quick Check:

    forEach async does not await = Using forEach with async does not wait for all fetches to finish. [OK]
Quick Trick: Avoid async inside forEach; use for...of or Promise.all [OK]
Common Mistakes:
MISTAKES
  • Assuming forEach waits for async callbacks
  • Not returning or awaiting promises properly
  • Misplacing results array declaration

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes