Reputation: 849
What is the best way to get the data from multiple observables before executing a function
EXAMPLE:
provider.ts
obs1 = this.afDB.object(`app/radio`).snapshotChanges();
obs2 = this.afDB.object(`app/users`).snapshotChanges();
obs3 = this.afDB.object(`app/song`).snapshotChanges();
app.ts
doSomething(){
console.log('this is my', ${radio}, 'it plays on this date:' ,${date}, 'this song', ${song}`);
}
i place the subscribtions inside eachother.. it works but.. what is the right way of handling this async behaviour?
doSomething(){
provider.obs1.subscribe(val1 =>{
provider.obs2.subscribe(val2 =>{
provider.obs3.subscribe(val3 =>{
console.log('this is my', ${val1.payload.val().radio}, 'it plays on this date:' ,${val2.payload.val().date}, 'this song', ${val3.payload.val().song}`);
});
});
});
}
Upvotes: 0
Views: 48
Reputation: 1531
use forkJoin operator.
doSomething(){
Observable.forkJoin(
provider.obs1,
provider.obs2,
provider.obs3
).subscribe(data => {
console.log("values: ",data[0], data[1], data[2])
}, err => {
console.log("Handle error here");
})
}
Upvotes: 1