Node.js · Async Programming · Mid-Level · Live Coding
How do you implement a timeout around an async operation?
Short Interview Answer
AbortController + fetch signal, or Promise.race with a timer that aborts/cancels underlying work.
Detailed Explanation
Racing alone without cancelation leaves work running. Prefer abortable APIs. Clear timers to avoid leaks.
Example
const ac = new AbortController();
setTimeout(() => ac.abort(), 5000);
await fetch(url, { signal: ac.signal });