Skip to main content
JavaScript Interview Questions Predict Output P0 Hard

Awaiting forEach with an async callback

Promise.resolve() is already fulfilled. Predict the order without assuming that forEach waits.

Official Reference

Code Snippet

async function demo() {
  await [1, 2].forEach(async id => {
    await Promise.resolve();
    console.log("item", id);
  });
  console.log("after");
}
demo();
console.log("end");

Expected Output & Explanation

end
item 1
item 2
after

The two callback continuations happen to be queued before demo’s continuation. forEach still returns undefined, not the callbacks’ promises. If they wait on timers or network requests, after can appear first. Use for…of with await for sequential work or Promise.all with map for concurrency.

Module Context & References

JavaScript · Async output drills Trace microtasks, promise identity, thenables and error recovery
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.