Re: [whatwg/dom] Add AbortSignal.any() (PR #1152)

> Why would that be the expectation? If you had a similar situation with promises and the event dispatch happens at resolve() you'd get the exact same results.

I don't think that's really a fair analogy since promises settle synchronously and react asynchronously so you get a "microtick" between each place `.any` or `.all` would block if you "unleash zalgo" and have promises that call their `then` handlers synchronously I would absolutely expect `41230` and not 012345`.

Namely, consider:

```js
function any(promises) {
  return new Promise(r => promises.map(x => x.then(r)));
}
```

As a naive implementation of _any_ without correct error semantics - with this implementatiion the following:

```js
{
    let resolve;
    const promise = new Promise(r => resolve = r);
    const promises = [];

    promises.push(promise);

    promises.push(any([promise]));
    promises.push(any([promise]));
    promises.push(any([promises[0]]));
    promises.push(any([promises[1]]));
    console.log(promises)
    let result = "";
    for (let i = promises.length - 1; i >= 0; i--) {
      promises[i].then(() => {
        result += i;
      });
    }
    resolve();
    await Promise.all(promises);
    console.log(result);
}
```

would log `01234`  since each `.then` callback is deferred a micro-tick.

On the other hand if we consider signals, the "straightforward" implementation which is:

```js
function any(...signals) {
  const ac = new AbortController();
  // ignore case already aborted for example
  signals.forEach(s => s.addEventListener("abort", e => ac.abort(e));
  return ac;
}
```

Would log 41230 since handlers use the same machinery as regular event listeners (addEventListeners).

Basically, using the existing machinery for promises leads to this order but using EventTargets does not.

This change fundamentally uses a way to subscribe to signals that is _different_ from what is accessible to userland and userland can't reproduce in a spec-complaint way on a regular EventTarget. This is unlike promises where the order is coonsistent.

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

Message ID: <whatwg/dom/pull/1152/c1532972088@github.com>

Received on Wednesday, 3 May 2023 12:48:19 UTC