Problem, as an interviewer would state it:
"Write promiseTimeout(promise, ms) — it should resolve/reject with whatever promise does, UNLESS ms milliseconds pass first, in which case it should reject with a timeout error instead."
Examples:
await promiseTimeout(fetch(url), 5000); // resolves normally if fast enough
await promiseTimeout(fetch(url), 5000); // rejects with a timeout error if too slow
Clarifying questions expected:
- If the timeout fires, does the original (now-abandoned) promise's own eventual result matter at all, or should it just be ignored?
- Should the timeout error be a specific, identifiable type/message, so callers can distinguish "timed out" from "the operation itself failed"?
- Is there a real, existing building block in this bank that already solves "whichever settles first wins"?
Code / implementation expected: Yes — real, direct proof of both outcomes: a fast promise correctly beating a timeout, and a genuinely slower promise correctly triggering it.