Re: [whatwg/dom] once() and on() async utilities for EventTarget (Issue #1038)

+1 to async iterator over ReadableStream; ReadableStream is designed for I/O (see e.g. [the FAQ](https://github.com/whatwg/streams/blob/main/FAQ.md), or the frequent analogy that arrays are specialized sync iterables for random access, whereas ReadableStreams are specialized async iterables for I/O).

I thought further on the naming question and think that although it's slightly verbose, if the names were `toPromise()` and `toAsyncIterator()` this would have a couple benefits:

- Very explicit
- Does not preclude using the clever/short names like `once()` or `next()` or `on()` for anything like #544 in the future, which would have full access to event semantics including cancelation
- It should be somewhat obvious that, because promises and async iterators are async, `event.preventDefault()` won't work.

I agree that it's not really desirable to combine these proposals with cancelability; for that we should continue to use `addEventListener()` for now, or something cool like #544 later.

I think race and all patterns already work pretty well with the base proposal? You end up with an extra event listener hanging around that is ultimately ignored; that isn't fully optimal, but in most cases it shouldn't matter:

```js
const e = await Promise.race([eventTarget.toPromise("foo"), eventTarget.toPromise("bar")]);
```

-- 
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/1038#issuecomment-985008915

Received on Thursday, 2 December 2021 21:19:17 UTC