Re: [whatwg/dom] Expose an `aborted` promise on AbortSignal? (#946)

> but we shouldn't over-index on them.

Agreed, to an extent. I don't think we should design a util that only serves my use case, but I also hope we don't design a util that falls short of it either. Would be nice to find a flexible compromise.

If the util you suggested had an option to be called without the `taskCallback(..)` param, and in that case, could *only* listen to the `signal`'s event... that affordance would make your suggested util *much* more friendly for my purposes.

For clarity, here's sorta what I'm suggesting:

```js
async function abortableTask(signal, taskCallback) {
  if (signal?.aborted) throw new DOMException('', 'AbortError');
  let onAbort, listener;
  const setOnAbort = (callback) => { onAbort = callback };
  const taskPromise = taskCallback?.(setOnAbort);
  const signalPromise =  new Promise((_, reject) => {
    listener = () => {
      onAbort?.();
      reject(new DOMException('', 'AbortError'));
    };
    signal?.addEventListener('abort', listener);
  });

  return (
    taskPromise ?
      Promise.race([ signalPromise, taskPromise ]) :
      signalPromise
  )
  .finally(() => signal?.removeEventListener('abort', listener));
}

// elsewhere
const ac = new AbortController();
ac.signal.pr = abortableTask(ac.signal);
```

With just a few changes here, it makes the `taskCallback(..)` param optional. If it's omitted, it's not called and the `Promise.race(..)` is skipped. I think that's a compromise that serves both sets of use-cases well... it doesn't detract from your favored "insides-only" approach, but gives a non-hacky escape hatch for the "outsides" need.

If instead I have to pass a function for `taskCallback(..)` that returns a never-resolving no-op promise, I've shot myself in the foot. Ostensibly, the purpose for me using this util would be to hopefully avoid/reduce the potential memory leakage, but now I'm creating a do-nothing promise that just hangs around (maybe forever?).

-- 
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/946#issuecomment-774705703

Received on Sunday, 7 February 2021 16:41:52 UTC