loganrussell48
loganrussell48

Reputation: 1864

How to convert Flux<Entity> to List<Mono<Entity>>

I actually have a List and I need to make a request for each one.

I need to wait for all the requests for the given list to complete.

If I create a Flux.fromIterable(entities) and then .flatMap(this::makeRequest) then I'm left with a Flux<Mono<ReturnType>>

One thing I've done already is Mono.when(entities.stream().map(this::makeRequest).collect(Collectors.toList()).block()

But I'm not sure if this is the best way, or if there's a way I can use Mono.zip or Flux.fromIterable. Any help finding the best way to do this would be great.

Upvotes: 0

Views: 355

Answers (1)

lkatiforis
lkatiforis

Reputation: 6255

There is no need to create a List. You can do the following:

Flux.fromIterable(entities)
    .flatMap(this::makeRequest) // perform request for each entry
    .then() //resulting in a Mono<Void> after all requests are completed

You can also use .collectList() instead of then if you want to list the responses.

Upvotes: 1

Related Questions