Re: [whatwg/dom] Consider adding AbortController.prototype.follow(signal) (#920)

> I wonder if we should flush out the use cases

@annevk, here's one I've been recently experimenting with: `AbortablePromise`. It creates a child `AbortController` to help with the proper garbage collection of event listeners, because the child `AbortController`/`AbortSignal` will go out of scope when the promise is resolved. 

In a nutshell ([full gist](https://gist.github.com/noseratio/b7de3c7383ae3660904d650cd418c2dd)):

```JavaScript
function delay(ms, outerSignal) {
  return new AbortablePromise(outerSignal, ({resolve, signal}) => {
    const id = setTimeout(resolve, ms);
    signal.addEventListener('abort', () => clearTimeout(id));
  });  
} 

const controller = new AbortController();
setTimeout(() => controller.abort(), 1000);
await delay(2000, controller.signal);
```

I haven't checked yet if everything gets GC'ed as expected, but it certainly would not without a child controller. 

This experiment was inspired by @jakearchibald's ["Event listeners and garbage collection"](https://jakearchibald.com/2020/events-and-gc/) - thanks Jake!
  

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/920#issuecomment-739071424

Received on Friday, 4 December 2020 23:15:05 UTC