Skip to solution
mediumDSA

What are Promises in Node.js and how do they improve async code?

957 views
01

Understand the problem

Question presented to candidate: "Explain what a Promise actually is — not just how to use .then() — and why it was worth adding to the language on top of plain callbacks."

What a strong answer should cover:

  • A Promise is an object representing the eventual result of an async operation — it exists in one of exactly three states: pending, fulfilled (with a value), or rejected (with a reason), and once fulfilled or rejected, it is permanently settled — it can never change state again.
  • .then(onFulfilled, onRejected) registers callbacks for the eventual outcome and itself returns a new Promise, which is what makes chaining (.then().then()) possible — each .then() can return a value, or another Promise, and the chain waits for it.
  • .catch(fn) is exactly .then(undefined, fn) — syntactic sugar for handling a rejection, and critically, it catches a rejection from any earlier step in the chain, not just the immediately preceding one, which is the main ergonomic win over per-step callback error handling.
  • Promise.all([...]) runs multiple Promises concurrently and resolves when all succeed (or rejects as soon as any one does); Promise.allSettled([...]) waits for all of them regardless of individual success/failure and reports each outcome — a distinct, commonly-confused-with-all tool.
  • Promise .then/.catch callbacks are scheduled as microtasks — they always run after the current synchronous code finishes and after process.nextTick's queue, but before the next macrotask (a setTimeout, an I/O callback) — this timing guarantee is itself a real, testable claim, not just a style preference.
  • async/await is not a different mechanism — an async function always returns a Promise, and await is syntax for consuming one; understanding Promises underneath is what makes async/await's behavior (including error propagation) predictable rather than magic.

Clarifying questions expected:

  • "Does the interviewer want the mechanics (states, microtask timing) or just usage patterns?" — these are different depths of the same topic.
  • "Is error handling for one step or the whole chain the actual concern here?" — decides whether to reach for a single trailing .catch or a .then(ok, err) pair at one step.

Code / implementation expected: Yes — a chain with a deliberate mid-chain rejection actually caught by a single trailing .catch, plus a real timing check proving the microtask-before-macrotask ordering, is the concrete deliverable.

promisesasynchronouserror handlinges6
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for Node.js/JavaScript interviews — assumes basic callback familiarity, no prior Promise knowledge required. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every timing and cha

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

A single try/catch catching a mid-chain rejection, plus the measured sequential-vs-Promise.all timing difference
function wait(ms, label) { return new Promise((r) => setTimeout(() => r(label), ms)); }

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

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
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 56 of 152 decoded in the Node.js track. One more won't hurt.

Back to track