Skip to the solution
MediumFrontend6 min read

How do you handle asynchronous operations in JavaScript?

784 views

The problem

Question presented to candidate: "You have a slow operation that finishes after a delay. Show me the same operation handled three genuinely different ways — and explain whether they're fundamentally different mechanisms, or just different syntax for the same thing underneath."

What a strong answer should cover:

  • 📌 Interview term: callbacks — the original, foundational approach: passing a function to be called once the async work completes — verified directly, still the real underlying mechanism every other approach genuinely builds on.
  • 📌 Interview term: .then() chaining — Promises (covered in this bank's own dedicated Promise-states question) wrap a callback-based operation in a more composable, chainable object.
  • 📌 Interview term: async/await (covered in this bank's own dedicated question) — syntax that lets Promise-based code be WRITTEN in a synchronous-looking style, while still genuinely running asynchronously underneath.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly: the identical delayed task, run through all three approaches side by side, genuinely produces the same real result — confirming they are different SYNTAX over the same underlying event-loop/task-queue mechanism, not three fundamentally different ways of achieving concurrency.
  • A precise answer names the real, practical trade-offs each style has: callbacks genuinely risk deep nesting ("callback hell") for sequential dependent operations; .then() chaining flattens that nesting but still requires an explicit .catch() for errors; async/await genuinely reads most like synchronous code and lets a plain try/catch handle errors, at the real cost of needing to be careful about accidentally serializing genuinely-independent operations (covered in this bank's own async/await question) instead of using Promise.all.

Clarifying questions expected:

  • None — this is a definitional/survey question; directly answering whether the three approaches are fundamentally different (they're not — same underlying mechanism) is the strong signal.

Code / implementation expected: Yes — the identical delayed operation, genuinely run through all three approaches side by side, is the clearest, most convincing demonstration that they're the same mechanism underneath.

  • async
  • promises
  • async/await
  • callbacks

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 · 7 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: the identical delayed operation, run through callbacks, .then() chaining, and async/await, all produce the same correct real result

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 66 of 165 in JavaScript

Keep your momentum going.

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

Up nextMedium

How do you implement a custom Promise.all polyfill?

Continue