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)— callsfnand returns a real Promise reflecting its outcome — verified directly: whetherfnreturns a plain synchronous value, throws synchronously, or itself returns a Promise (like anasync function),Promise.trygenuinely 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 inPromise.resolve().then(fn)ALSO catches a synchronous throw (since it defersfn's call into a.then()callback), but genuinely adds a real, unnecessary extra microtask tick delay beforefneven runs, compared toPromise.try's more direct approach. - A precise answer names that
Promise.trygenuinely 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 realasync 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.