Peter Jurkovic
Peter Jurkovic

Reputation: 2896

How to asynchronosuly reduce a Flux to Mono with Reactor

I am trying to figure out how to asynchronously do a reduction of Flux to Mono.

Here is an example:

interface AsyncTask {
    Mono<Result> apply(Result resutl);
}

List<AsyncTask> tasks = ...

Flux.fromIterable(tasks)
    .reduceWith(Result::new, (r, task) -> {
        // This does not compile, it expects Result, not Mono<Result>
        return task.apply(r)
    })

The goal is to sequentially apply a task to a given result and use the result for the next task in the list.

Upvotes: 0

Views: 966

Answers (1)

echooymxq
echooymxq

Reputation: 96

Flux.fromIterable(tasks)
    .reduceWith(() -> Mono.just(new Result()), (result, task) -> {
         return result.flatMap(task::apply);
    });

Upvotes: 1

Related Questions