I want to be able to wait for a List of Monos to get resolved and upon completion of the last element, proceed with another async call
public Mono<Artist> getArtistInfo(String id) {
//Call API1
Mono<MusicResponse> musisResponseMono = webClientBuilder
.build()
.get()
.uri(uri + "\\" + id)
.retrieve()
.bodyToMono(MusicResponse.class);
//1.async handler for the first call
return musisResponseMono.flatMap(musicRes ->{
Artist artist = new Artist();
List<Albums> albums = musicRes.getAlbums();
//make mutiple concurrent API calls to API2
albums.stream().forEach( album -> {
webClientBuilder
.build()
.get().uri("API 2 URL")
.retrieve()
.bodyToMono(Covers.class)
.subscribe(cover -> artist.getAlbums().add(cover.getImage()));
});
//call API3 - want to wait untill all API calls 2 are completed
return webClientBuilder
.build()
.get()
.uri("API3 URL")
.retrieve()
.bodyToMono(Profiles.class)
.map( profileRes ->
artist.setDescription(profileRes.getDescription())
);
}
}
The problem is that API call 3 may return before every element of the second call is returned . I guess what I'm looking for is something like asycn await in Javascipt in the context of Spring Webflux