Problem, as an interviewer would state it:
"Implement a cancelable async operation using AbortController/AbortSignal — aborting it should genuinely stop the underlying work, not just have the caller ignore a late result."
Examples:
const controller = new AbortController();
const p = cancelableDelay(5000, controller.signal);
controller.abort(); // p rejects almost immediately, the real underlying timer is cleared
Clarifying questions expected:
- Does "cancel" mean genuinely stopping the underlying work (clearing a timer, aborting a real fetch), or just having the CALLER ignore a result that still arrives later?
- What should happen if the signal is ALREADY aborted before the operation even starts?
- Should the rejection use a specific, real, standard error type/name, for callers to distinguish an intentional cancellation from a genuine failure?
Code / implementation expected: Yes — real, direct proof that aborting mid-flight causes a genuinely FAST rejection, not one that waits for the original, full duration to elapse.