Closed domenic closed 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...
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.
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.
toArray()
is specced to return anObservable
, but per its name, I think it should return anArray
(like iterator helpers) or aPromise<sequence<any>>
(like async iterator helpers).forEach()
is specced to return anObservable
, but I would expect it to either returnundefined
(likeArray.prototype
/ iterator helpers) orPromise<undefined>
(like async iterator helpers)