I want to make multiple parallel API calls using the same endpoint but different parameters. And subscribe to it if any of the request is complete. How can I handle this. This is my current approach. I am unsure of how to make parallel api calls and subscribe to it sequentially. Please suggest the better approach.
getProductDetails(id) {
return this.http.get(
`https://dummyjson.com/products/get_details?id=${id}`
);
}
createProductDetails(productDetails) {
}
ngOnInit() {
const ids = ["a","b", "c", "d", "e", "f"];
// api calls should happen in parallel manner
from(ids).pipe(mergeMap((item) => {
return this.getProductDetails(item)
})).subscribe(response => {
// it should create product details whenever response for any of the product is ready
// this function should get called 6 times since their are 6 products
this.createProductDetails(response)
});
}
End goal is to make parallel api calls and start showing data as soon as the response is ready for any of the item
2
Answers
I am not quite sure what the purpose of the pipe is in this situation. But I would suggest something like this using simple JavaScript promises.
If you want to know when all promises have completed you can change forEach into map like so.
I think you are looking for CombineLatest
In this you can calls multiple API’s simultaneously and get the response in one shot.