Question presented to candidate: "You want a fetch request that can be cancelled either by the user clicking a 'Cancel' button OR automatically after 5 seconds, whichever happens first. How would you wire that up without hand-rolling a manual setTimeout-based abort?"
What a strong answer should cover:
- 📌 Interview term:
AbortSignal.timeout(ms)— a real, built-in static method that genuinely returns anAbortSignalwhich automatically aborts itself after the given number of milliseconds — verified directly, timed against a real slow endpoint, aborting at approximately the requested delay with a realDOMExceptionnamed"TimeoutError". - 📌 Interview term:
AbortSignal.any(signals)— a real, built-in static method that genuinely combines an array of signals into ONE new signal, which aborts the moment ANY of the input signals abort — verified directly two ways: a manual controller's custom-reason abort winning the race, and separately a timeout's realTimeoutErrorwinning when the manual controller was never triggered. - 📌 Interview term: the real, direct answer to the prompt — verified directly:
AbortSignal.any([manualController.signal, AbortSignal.timeout(5000)])produces exactly the combined signal the prompt describes — passed tofetch()'s{ signal }option, the request is genuinely cancelled by whichever of the two fires first, with the combined signal's real.reasonreflecting whichever ACTUALLY won. - 📌 Interview term: the real reason-propagation guarantee — a precise answer names that the combined signal's
.reasonis genuinely NOT a generic "combined" value — it is EXACTLY the winning input signal's own real reason (a custom string, or a realTimeoutError), letting calling code distinguish which trigger actually fired. - A precise answer names the real, practical value over a hand-rolled version: no manual
setTimeout/clearTimeoutbookkeeping is needed, and no risk of a real, common bug where a manual timeout fires AFTER a manual cancel already happened (a stale timeout callback double-aborting or referencing cleaned-up state).
Clarifying questions expected:
- None — this is a definitional/practical question; directly wiring up and demonstrating the prompt's own exact scenario with real, verified proof is the strong signal.
Code / implementation expected: Yes — a real AbortSignal.any() combining a manual controller and a real AbortSignal.timeout(), verified with both possible winners of the race.