最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the difference between async generators and Observables? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked Jan 30, 2018 at 0:52 robbierobbie 1,3191 gold badge12 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

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 remend the General Theory of Reactivity as a good read.

I believe the answer could be found in their definition. A Generator function has the ability to stop and then continue later. An Observable can also stop and continue later but you need to subscribe to it first for it to begin.

First Difference - A generator executes when that function is called. An Observable technically only begins to execute or emit values when you subscribe to it.

发布评论

评论列表(0)

  1. 暂无评论