Skip to the solution
MediumDSA6 min read

How does async/await work and what are its benefits?

858 views

The problem

Question presented to candidate: "You see 'async' in front of a function and 'await' inside it. What does the JavaScript engine actually do with those keywords, and does await block the whole program?"

What a strong answer should cover:

  • An async function always returns a Promise β€” even a plain return 42 inside one becomes a Promise that resolves to 42, and a thrown error becomes a rejected Promise, verifiably, not just by convention.
  • await pauses execution only within that async function β€” it does not block the thread, the event loop, or any other concurrently running code. Control returns to the event loop while the awaited Promise settles, and other work (other requests, timers, other async functions) proceeds normally in the meantime.
  • async/await is syntax over Promises, not a separate mechanism β€” this is why try/catch works around an await: a rejected Promise being awaited is exactly equivalent to a thrown error at that point in the function.
  • The main practical benefit is readability: sequential async logic reads top-to-bottom like synchronous code, with ordinary control flow (if, loops, try/catch) working exactly as it does in synchronous code β€” no .then() nesting or chaining gymnastics required.
  • The most common real bug is writing sequential awaits for operations that do not actually depend on each other, silently serializing work that could run concurrently via Promise.all β€” this is a behavior/performance bug, not a syntax error, so it does not surface as an obvious mistake.
  • for await...of extends the same idea to async iterables (including Node streams) β€” pulling the next value only once the previous iteration's work, including any await inside the loop body, actually finishes; this interacts directly with stream backpressure, covered in its own dedicated question.

Clarifying questions expected:

  • "Are these steps sequential-by-dependency, or just written sequentially out of habit?" β€” decides whether Promise.all should replace some of the awaits.
  • "Does the interviewer want proof that await does not block other code, or just the syntax explained?" β€” these call for different depths of answer.

Code / implementation expected: Yes β€” showing an async function's return value actually being a Promise, and a thrown error becoming a rejection caught by an ordinary try/catch, is the concrete, verifiable core of the answer.

  • async/await
  • promises
  • asynchronous
  • readability

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Read the code

JavaScriptAn async function's return value is a real Promise object, and a thrown error becomes a rejection caught by try/catch
async function f() { return 42; }
console.log(f());        // Promise { 42 } β€” not the number itself
console.log(await f());  // 42

async function mayReject(shouldFail) {
  if (shouldFail) throw new Error("boom");
  return "ok";
}
try {
  await mayReject(true);
} catch (e) {
  console.log("caught:", e.message); // caught: boom
}

// The accidental-serialization trap, measured:
function wait(ms) { return new Promise((r) => setTimeout(r, ms)); }
const t0 = Date.now();
await wait(100); await wait(100); await wait(100);
console.log("sequential:", Date.now() - t0, "ms"); // sequential: 315 ms

const t1 = Date.now();
await Promise.all([wait(100), wait(100), wait(100)]);
console.log("Promise.all:", Date.now() - t1, "ms"); // Promise.all: 108 ms

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 62 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextMedium

What are `EventEmitter`s in Node.js?

Continue