demo · v131

cancellation: AsyncDisposable wrapping an AbortController

When a request scope ends — user navigates away, drawer closes, page tears down — every in-flight fetch should abort. Wrap AbortController as an AsyncDisposable and any function leaving the scope sends abort to all child requests. Mock a slow API and watch in-flight requests cancel together.

scope log

in-flight: 0 aborts: 0 completed: 0 last reason: none

request timelines

the api

class RequestScope {
  constructor() { this.ctrl = new AbortController(); }
  fetch(url) { return fetch(url, { signal: this.ctrl.signal }); }
  async [Symbol.asyncDispose]() { this.ctrl.abort("scope ended"); }
}

async function run() {
  await using scope = new RequestScope();
  scope.fetch("/api/users");
  scope.fetch("/api/feed");
  // ... scope ends here, both in-flight requests abort
}

see also