我希望能够等待 Monos 列表得到解决,并在完成最后一个元素后,继续进行另一个异步调用
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())
);
}
}
问题是 API 调用 3 可能在返回第二个调用的每个元素之前返回。我想我正在寻找的是 Spring Webflux 上下文中 Javascipt 中的 asycn await 之类的东西
哔哔one
相关分类