Skip to content

About

a Java version of Javascript async generator

Resources

Stars

17 stars

Watchers

2 watching

Forks

Repository files navigation

java-async-generator

A Java version of Javascript async generator. Idea is to create an iterator-like interface that emit elements as CompletableFuture ( the Java counterpart of Javascript Promise ) enabling asynchronous iteration over data

Releases

Note: ‼️

From release 3.0.0 the miminum supported Java version is the Java 17 and will not be longer available the artifact async-generator-jdk8

Installation

Maven

<dependency>
    <groupId>org.bsc.async</groupId>
    <artifactId>async-generator</artifactId>
    <version>5.2.1</version> <!-- Or the current release version -->
</dependency>

Building

Use the included Maven Wrapper, pinned to Maven 3.9.16, with JDK 17 or newer:

./mvnw --version
./mvnw -B clean verify

On Windows, use mvnw.cmd instead of ./mvnw. The wrapper downloads Maven on first use. GitHub workflows also use the wrapper to keep the Maven version consistent.

To publish a signed release with your Central credentials and GPG key configured:

./mvnw -B -Prelease clean source:jar javadoc:jar deploy

Use ./mvnw for releases to avoid the Maven 3.10 staging metadata incompatibility with the current Central Publishing Plugin. Include clean to remove previous staging files.

Samples

Create an Async Generator to make multiple API calls

AsyncGenerator<Response> makeMultipleApiCalls(List<RequestData> requestsData) {
    return AsyncGenerator.map(requestsData, requestData -> {

                CompletableFuture<Response> res = asyncApiCall( requestData );

                return res;
    });
    
}

List<RequestData> resquestsData = .... 

// can iterate using lambda function (Consumer)
makeMultipleApiCalls( resquestsData )
        .forEachAsync( response -> logger.info( "Api response: " + response ) )
        .join();

// can iterate using classic for( : )
AsyncGenerator<Response> generator = makeMultipleApiCalls( resquestsData );

for( Response response : generator ) {
    logger.info( "Api response: " + response )
}        

Cancellation

Take a look the new Cancellation feature

Articles:

About

a Java version of Javascript async generator

Resources

Stars

17 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages