I have following pseudocode
objects = [obj1, obj2, obj3]
for obj in objects {
user: http.get(obj1.user_id)
product: http.get(obj1.product_id)
}.subscribe(results => {
do something with results
})
What I want here is that, I loop through an array of objects and for each object get related user and product and then subscribe to do something with the user and product. How can I do that?
You could use RxJS forkJoin
along with Array#map
to trigger multiple observables in parallel.
const objects = [obj1, obj2, obj3];
forkJoin(
objects.map(obj => // <-- `Array#map` function
forkJoin({
user: http.get(obj.user_id)
product: http.get(obj.product_id)
})
)
).subscribe({
next: (res) => {
console.log(res);
// use `res`
},
error: (err) => {}
});
/*
output in subscription:
[
{
user: result from `http.get(obj1.user_id)`,
product: result from `http.get(obj1.product_id)`,
},
{
user: result from `http.get(obj2.user_id)`,
product: result from `http.get(obj2.product_id)`,
},
{
user: result from `http.get(obj3.user_id)`,
product: result from `http.get(obj3.product_id)`,
}
]
*/
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments