Say I have two observables obs1
and obs2
.
obs1.subscribe(res => { //do stuff with output1});
obs2.subscribe(res => { //do stuff with output2});
I want to subscribe to obs2
only after obs1
is finished execution. The only way I know is ugly:
obs1.pipe(finalize(() => {
obs2.subscribe(res => { //do stuff with output2});
})).subscribe(res => {//do stuff with output1})
Is there a more elegant way? Adding a third obs3
would make code very hard to read.
EDIT: From previous answers it was not clear to me how to have data from individual observables. The following code does exactly what I want:
concat(
obs1.pipe(tap( res => console.log(res))),
obs2.pipe(tap( res => console.log(res)))
).subscribe()
What previous answers suggested was:
concat(obs1, obs2).subscribe(res => console.log(res))
but in this case the final response merges responses of obs1
and obs2
and I did not know how to separate it.