Skip to solution
mediumFrontend

What does `Promise.try()` do and why is it better than `Promise.resolve().then(fn)`?

317 views
01

Understand the problem

Question presented to candidate: "If a function throws SYNCHRONOUSLY (not inside any Promise), does wrapping the call in .catch() somewhere catch that error? What does Promise.try(fn) actually do differently from just calling fn() directly?"

What a strong answer should cover:

  • 📌 Interview term: Promise.try(fn) — calls fn and returns a real Promise reflecting its outcome — verified directly: whether fn returns a plain synchronous value, throws synchronously, or itself returns a Promise (like an async function), Promise.try genuinely handles ALL THREE cases uniformly, always producing a real Promise.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly: calling a synchronously-throwing function DIRECTLY (with no Promise.try) genuinely throws IMMEDIATELY, synchronously — a .catch() attached anywhere genuinely does not catch it, since the throw happens before any Promise machinery is even involved. Promise.try(fn), by contrast, genuinely catches that same synchronous throw and converts it into a real Promise rejection, catchable normally.
  • 📌 Interview term: Promise.resolve().then(fn)'s real limitation — a precise answer names the older workaround this method replaces: wrapping a call in Promise.resolve().then(fn) ALSO catches a synchronous throw (since it defers fn's call into a .then() callback), but genuinely adds a real, unnecessary extra microtask tick delay before fn even runs, compared to Promise.try's more direct approach.
  • A precise answer names that Promise.try genuinely unifies handling of synchronous AND asynchronous functions behind ONE consistent Promise-returning interface — verified directly working correctly for both a plain synchronous function and a real async function.

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering whether a bare synchronous throw is caught (it isn't, without Promise.try) is the strong signal.

Code / implementation expected: Yes — the direct contrast between a bare synchronous throw (uncaught by any .catch()) and Promise.try's genuinely caught, converted rejection is the clearest demonstration.

promiseses2024error-handling
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 JavaScript/async interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every throw and catch path below was actually run in Node.

1. Why This Even Matters — A Stor

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: a bare synchronous throw genuinely escapes any .catch(), while Promise.try genuinely catches the identical throw and converts it into a real rejection
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 100 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track