Skip to content

Alternative to Observables #11

@appden

Description

@appden

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. 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions