Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between async generators and Observables?

Async generators: An example case is a readable stream

Observables: A fundamental protocol for processing asynchronous streams of data

These both seem like different ways of tackling the same problem of an asynchronous stream of data. Is there a practical difference between the two, besides a matter of taste?

like image 396
robbie Avatar asked Jan 30 '18 00:01

robbie


1 Answers

Judging from the proposed API descriptions:

  • observables can have multiple subscribers (broadcast), asynchronous iterators can only have a single reader (unicast)
  • observables push the events, while asynchronous iterators need to be polled
  • admittedly, the lazy nature of the Observable constructor does blur the lines

Observables are basically event emitters, while asynchronous iterators can be used to form a streaming flow. I also recommend the General Theory of Reactivity as a good read.

like image 94
Bergi Avatar answered Sep 28 '22 02:09

Bergi