Skip to content
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

Alternative to Observables #11

Open
appden opened this issue Feb 6, 2015 · 5 comments
Open

Alternative to Observables #11

appden opened this issue Feb 6, 2015 · 5 comments

Comments

@appden
Copy link

appden commented Feb 6, 2015

Hi, I fully admit I might be wrong or have missed something from reading this proposal, but please hear me out...

I'm quite convinced that having a async generator would very nicely complement async functions and generators within the language. However, I'm not convinced of the need for Observable or even the for-on syntax. If given an iterator that iterates over promises, consuming it is easy:

for (let promise of socket) {
    let price = await promise;
    ...
}

The harder part is actually creating that iterator that returns a promise for the next result. The code to properly accomplish that is quite convoluted, and could be vastly simplified with your proposed async generators. That is why I'd propose that an async generator actually return an iterator over promises for the next result.

This would greatly simplify this proposal, and I think give it a better shot of making it into ES7 as well as reduce the amount of new syntax and cognitive overhead for learners of the language. I agree with others' sentiment that for-on is too similar to in and of and a bit hard to spot in the code given its radically different behavior. I also agree that Observable would have some uses, but is better left to third-party libraries to implement and makes this proposal much heavier than it needs to be.

I also think returning an iterator would allow for very flexible patterns using iterator libraries with similar functionality to Python's itertools combined with await to accomplish amazing feats with very little code. 😄

@zenparsing
Copy link

Hi @appden

It's not quite that simple, because the done-ness of the sequence is itself asynchronous. You don't know whether to exit the loop until the promise is resolved.

See https://github.com/zenparsing/async-iteration#the-asynciterator-interface for more along those lines.

The main issue brought up against AsyncIterators is that they imply the overhead associated with promises and request queuing. Observables simply call their observer using a regular function call, so overhead is very low.

@appden
Copy link
Author

appden commented Feb 8, 2015

Thanks @zenparsing, that makes a lot of sense. Still, I'm a fan of your AsyncIterator proposal.

@calvinmetcalf
Copy link

@zenparsing what is the relationship between your proposal and this one, is your version an alternate proposal?

@zenparsing
Copy link

@calvinmetcalf It's an alternate proposal that I developed a few months ago. Since then, I've come to think that observables are really great, but my personal opinion is that they should be implemented on top of async iterators. I don't speak for anyone other than myself though, and I'm not the feature champion.

@calvinmetcalf
Copy link

yeah observables have a lot of overlap with streams so seem a bit highlevel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants