ReactiveX / rxjs

A reactive programming library for JavaScript
https://rxjs.dev
Apache License 2.0
30.73k stars 3k forks source link

Observable should implement Symbol.asyncIterator, (@@asyncIterator) #5482

Closed richytong closed 4 years ago

richytong commented 4 years ago

I like observables. Observables are a tried and true push source (thanks for testing them out so thoroughly), and they are modeled by async iterables. It's only natural that Observable (like a nodejs Stream) implement [Symbol.asyncIterator]

Right now, this is the case

> of(1)
Observable { _isScalar: false, _subscribe: [Function (anonymous)] }
> of(1)[Symbol.asyncIterator]
undefined

A solution I'd like

> of(1)
Observable { _isScalar: false, _subscribe: [Function (anonymous)] }
> of(1)[Symbol.asyncIterator]
[GeneratorFunction] // or any async iterable

You'd be able to use an Observable like

for await (const event of new Observable(...)) {
  // do stuff with event
}

I understand this is perhaps not the way you intended Observables, but @@asyncIterator is part of the ECMA spec now.

I am happy to help / contribute.

kwonoj commented 4 years ago

https://github.com/ReactiveX/rxjs/pull/5297