Reputation: 936
I have a situation where a service returns a number of Observables in an array. I need to subscribe to them in such a way where one is subscribed after the previous is complete.
I have done a similar thing before with the concat
operator, and it works great, but it only works if you put the observables as the parameters of the concat
signature like this. concat(obs1, obs2, obs3)
But if you have concat([obs1, obs2, obs3])
the behaviour changes completely.
concat(
of(1, 2, 3).pipe(delay(3000)),
of(4, 5, 6).pipe(delay(3000)),
).pipe(
toArray()
)
.subscribe((v) => {
// Gets called after 6 seconds containing all the data as number[].
console.log(v);
});
const obs = [of(1, 2, 3).pipe(delay(3000)), of(4, 5, 6).pipe(delay(3000))];
concat(obs).pipe(
toArray()
)
.subscribe((v) => {
// I want to get the same behaviour as before, but instead of v bein number[]
// it comes as an Observable<number>[]
console.log(v);
});
How can I make it so the concat function works the same way as the code example? Having only the resulting data as the subscription callback.
Upvotes: 0
Views: 167
Reputation: 2987
Use spread operator:
const obs = [of(1, 2, 3).pipe(delay(3000)), of(4, 5, 6).pipe(delay(3000))];
concat(...obs).pipe(toArray()).subscribe((v) => console.log(v);});
Upvotes: 2