# Async Iterable/Enumerable vs. Reactive-Streams

DevFeed: [Async Iterable/Enumerable vs. Reactive-Streams](<https://devfeed.tech/articles/async-iterable-enumerable-vs-reactive-streams-24803.md>)

Original publisher: [Read original article](<https://akarnokd.blogspot.com/2016/05/async-iterableenumerable-vs-reactive.html>)

Author: David Karnok (noreply@blogger.com)

Published: 2016-05-02T13:54:00Z

Content type: article

Language: en

Sources: [Akarnokd - Advanced RxJava](<https://devfeed.tech/sources/akarnokd-advanced-rxjava.md>)

Topics: [Reactive Streams](<https://devfeed.tech/topics/reactive-streams.md>), [reactive](<https://devfeed.tech/topics/reactive.md>), [interface](<https://devfeed.tech/topics/interface.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [async](<https://devfeed.tech/tags/async.md>), [backpressure](<https://devfeed.tech/tags/backpressure.md>), [cancellation](<https://devfeed.tech/tags/cancellation.md>), [completablefuture](<https://devfeed.tech/tags/completablefuture.md>), [deferred](<https://devfeed.tech/tags/deferred.md>), [implement](<https://devfeed.tech/tags/implement.md>), [interfaces](<https://devfeed.tech/tags/interfaces.md>), [iterable](<https://devfeed.tech/tags/iterable.md>), [java](<https://devfeed.tech/tags/java.md>), [java-8](<https://devfeed.tech/tags/java-8.md>), [library](<https://devfeed.tech/tags/library.md>), [memory](<https://devfeed.tech/tags/memory.md>), [pipeline](<https://devfeed.tech/tags/pipeline.md>), [protocol](<https://devfeed.tech/tags/protocol.md>), [reactive](<https://devfeed.tech/tags/reactive.md>), [reactive-streams](<https://devfeed.tech/tags/reactive-streams.md>), [rxjava](<https://devfeed.tech/tags/rxjava.md>), [sequence](<https://devfeed.tech/tags/sequence.md>), [solve](<https://devfeed.tech/tags/solve.md>)

## AI overview

Async Enumerables provide backpressure by returning an asynchronous result from `moveNext()` and letting a consumer request the next element only after processing the current one. The excerpt's Java 8 API pairs `IAsyncEnumerable<T>` with enumerators whose `moveNext(CompositeSubscription)` returns a `CompletionStage<Boolean>`; `true` means a value is available through `current()`, and `false` means the sequence ended. The author describes this as an alternative to the non-blocking, request-coordinating Reactive-Streams protocol and plans to compare it with Reactive-Streams-Commons, but the excerpt gives no performance findings. It also notes that cancellation composition remains uncertain because Java `CompletionStage` lacks the disposal behavior cited for Ix.NET.

## Source excerpt

Introduction Backpressure is essential if one wants to avoid buffer bloat and excessive memory usage if two stages in a reactive pipeline consume events with different speed. RxJava and Reactive-Streams developed a non-blocking, request-coordinating protocol to solve this problem, but you may have heard there are alternatives to it. One alternative that comes up from time to time is Async Iterables (Java terminology) or Async Enumerables (C# terminology). In fact Rx.NET has an Ix.NET (stands for Interactive Extensions) sub-project in which there is the Async Enumerables library. It solves this backpressure problem by having a Task (~ CompletableFuture, ~ Promise) returned from its MoveNext() (~ hasNext()) method and when that Task fires, you can consume the Current property (~ next() method). The backpressure behavior comes from the fact that you'd call MoveNext() again only after you processed the the current element. Unfortunately, I haven't found a Java implementation for the IAsyncEnumerable (haven't really looked beyond a few Google searches), so I decided I'll implement it on my own in Java 8, see what it takes to get data across with it and how performant is it compared to my current cutting-edge understanding of reactive-flows: the Reactive-Streams-Commons library. Base API Since Async Enumerables are designed in deferred execution in mind, the base API consists of two interfaces: interface IAsyncEnumerable<T> { IAsyncEnumerator<T> enumerator(); } interface IAsyncEnumerator<T> { CompletionStage<Boolean> moveNext(CompositeSubscription cancel); T current(); } The IAsyncEnumerable is the equivalent of Iterable and it hands out IAsyncEnumerators. IAsyncEnumerator has a moveNext method which returns a CompletionStage indicating if there is value available via current() (signals true) or the sequence ended (signals false). C# CancellationToken looks like our CompositeSubscription so I'm reusing it as the way for cancellation. (Sidenote: I'm not sure how cancellati