-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Symbol.asyncIterator on Observable #6857
Comments
Core Team: silently approved for 8.x |
In a And if we want it to be cancellable from outside the for loop, we could somewhat easily stick a This could even be all wrapped up neatly with (rough proof of concept):
The My point is, while it's not currently natively cancellable, there are already ways to work around that right now, I guess... |
I believe this will make code confusing, especially for those who never used an Observable as an iterable. Consider the following: for await (const a of $myObservable) {
// set of operations
} It would not be a typical way to use an Observable, but rather unique. So when someone sees such code, he will be confused as to what is happening here underneath. Considering that such functionality won't be used often, a dedicated method is better, like for await (const a of $myObservable.iterate()) {
// set of operations
} Now we know, from this code signature, that we are working with an iterable here, no more mystery or confusion. Also, by making Observable an AsyncIterable, you effectively announce it as a good/right usage pattern for observables, which it is totally not, it is just a convenience, to be used where it is necessary, and not something to be abused, and rest assured, it will be. Please do not introduce bad usage patterns, add explicit method If you ignore my advice, I guarantee that lots of junior developers will end up writing terrible code, by using observables as iterables everywhere. It will create a disaster. With method Also, without dedicated method, I can easily foresee that code-styling libraries will be adding a new rule to forbid implicit use of observables as iterables. Another reason why it is best to avoid such practice. |
Discussed in #6779
Originally posted by benlesh January 21, 2022
Thinking about the backpressure-related use cases for interop between async iterables and observable, I think I'd consider it an improvement to ergonomics to implement
Symbol.asyncIterator
onObservable
. See this comment here about handling backpressure. There are some really cool/easy/clever things that can be done with this functionality.Here's a few things to consider:
concatMap
has exactly the same issue where users need to "understand there is buffering" in some cases, and so far, I haven't seen many people trip over that. In fact, many tutorials and documents steer people towardsconcatMap
for this buffered, one-at-a-time behavior more often than not.for await
isn't going away any time soon, and — other than callbacks — is the only real native way to iterate async values (one-at-a-time likeconcatMap
, of course).for await
is obviously non-cancellable, as there's no subscription or even an opportunity to pass a signal or the like, so it's unlikely to "replace" callingsubscribe
in the hearts and minds of users.For those new to this, here is what is being proposed (roughly):
Which would roughly map 1-to-1 with this RxJS behavior:
cc @cartant @kwonoj
The text was updated successfully, but these errors were encountered: