Time ago I wrote about Enumerables and async in C#. C# 8 will bring us this gift, same as last versions of JavaScript. In C# this feature made up by the IAsyncEnumerable/IAsyncEnumerator and the new await foreach loop has been called Async Streams. In JavaScript the feature is called async iterator. The Async Stream naming used in C# got mixed in my head with other terms like push streams, pull streams, synchronous, asynchronous, so I tried to clean up my mind a bit thinking about examples of each case. I'll use the term "stream" in a generic way, not directly associated to C# or Java stream classes. I could just have use the term "sequence".
- Synchronous Pull Streams. Just a conventional enumeration/iteration. An object that can provide several items. We can iterate it by requesting an item and synchronously wait for it. IEnumerable/IEnumerator, [symbol.iterator]... you know.
- Asynchronous Pull Streams. Just the new C#/JavaScript feature I was talking above. In each iteration we request an item and we asynchronously wait for it.
- Asynchronous Push Streams. An Observable is the perfect example. We subscribe to it and it will push data to us in an asynchronous way.
- Synchronous Push Streams. This is not so obvious, but Observables also fit here. While we normally think of observables as a data source providing data over a certain period, an Observable can also be synchronous. We subscribe to it and immediatelly it pushes to us all its items. Do you want an example?
console.log("starting"); of("Celine", "Francois").subscribe(nm => console.log("Hi " + nm)); console.log("finishing"); //output: //starting //Hi Celine //Hi Francois //finishing
No comments:
Post a Comment