`AbortController` provides a standard way to cancel an in-progress async operation — most commonly a `fetch` call that's taking too long, but also usable for any custom async function that checks `signal.aborted`.
js
async function fetchWithTimeout(url, ms) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), ms);
try {
const res = await fetch(url, { signal: controller.signal });
return await res.json();
} finally {
clearTimeout(timer);
}
}Without a timeout mechanism like this, a single slow or hung upstream RPC endpoint can leave requests (and their resources) pending indefinitely — a real risk when your service depends on external nodes you don't control.