For instance, assume I have 2 API services that return data in the form of Observables.
function add(row) {
let r = Math.ceil(Math.random() * 2000);
let k = row + 1;
return timer(r).pipe(mapTo(k));
}
function multiple(row) {
let r = Math.ceil(Math.random() * 2000);
let k = row * 10;
return timer(r).pipe(mapTo(k));
}
Now, with an array [1, 2, 3, 4, 5]
, I am using the above functions in the following way:
from([1, 2, 3, 4, 5]).pipe(
mergeMap((row) => {
return add(row);
}),
mergeMap((row) => {
return multiply(row);
}),
toArray()
).subscribe((_) => {
console.log("sub", _);
});
The resulting output is
sub [ 50, 20, 60, 40, 30 ]
This outcome meets my requirements. However, I am unsure about how to link the source elements from the array to their corresponding results.
I hope for a result like
[ [4, 50], [1, 20], [5, 60], [3, 40], [2, 30] ]
or
[
{ sourceData: 4, result: 50 },
{ sourceData: 1, result: 20 },
...
]
If I use concatMap to maintain the sequence, the program will execute sequentially. However, I am more concerned with connecting the source and the result rather than preserving the order.
Thank you very much~