WICG / observable

Observable API proposal
https://wicg.github.io/observable/
Other
563 stars 13 forks source link

Return types of some operators seem off #27

Closed domenic closed 1 year ago

domenic commented 1 year ago
domfarolino commented 1 year ago

toArray()'s return was inspired by RxJS which returns an observable that completes with the final array, is my understanding. But yeah if it is supposed to be a one-shot completion anyways, perhaps we should change that to Promise. @benlesh WDYT?

Agree that forEach() seems off...

benlesh commented 1 year ago

I'm on the fence here. A use case for toArray() is that you want to collect all of the observed values, then do something with those. And if you're composing your reactivity, it's best if it doesn't cause subscription.

However, there's definitely also a use case where people just want to collect all of the values and use them in some imperative code (like in an async function).

I do agree that toArray returning Observable is inconsistent with other methods that are following the iterator-helpers and returning promises (in lieu of values).

I'm also okay with omitting toArray() for now.

bakkot commented 1 year ago

Naively I would expect toArray to give you a promise which fulfills once the observable completes, and at that time holds an array of all of the values emitted since the call to toArray.

I'm on the fence here. A use case for toArray() is that you want to collect all of the observed values, then do something with those. And if you're composing your reactivity, it's best if it doesn't cause subscription.

@benlesh Can you say more about that second sentence? I'm not totally sure what "composing your reactivity" means - is the idea that the user wants to describe a pipeline without actually kicking anything off yet?

The iterator helper toArray will immediately pull from the underlying iterator (and keep pulling until the iterator finishes), so if the idea is indeed that sometimes you want something which doesn't have any immediate effects, toArray is probably not a good name for it.