Ad

Wait End Of Two Subscribe To Make An Operation

I have two subscribe like this :

this.birthdays = await this.birthdaySP.getBirthdays();
this.birthdays.subscribe(groups => {
    const allBirthdayT = [];
    groups.map(c => {
      allBirthdayT.push({
        key: c.payload.key, 
        ...c.payload.val()
      })
    })

    console.log(allBirthdayT);
});

this.birthdaysInGroups = await this.birthdaySP.getBirthdaysInGroups();
this.birthdaysInGroups.subscribe(groups => {
    const allBirthdayB = [];
    groups.map(c => {
      c.birthdays.subscribe(d => {
        d.map(e => {
          allBirthdayB.push(e);
        })
      })
    })

    console.log(allBirthdayB);
});

I would like to wait the end of this two subscribes to compare allBirthdayB and allBirthdayT arrays (i receive datas in two console.log).

this.birthdaySP.getBirthdays() and this.birthdaySP.getBirthdaysInGroups() are two observable that receive data from firebase.

The first Observable is like that :

async getBirthdays() {
   const user = await this.authSP.getUserInfo();
   return this.angularFire.list('birthdays', ref => ref.orderByChild('creator_user_id').equalTo(user.uid)).snapshotChanges();
}

I try with forkJoin but i don't know how i can use it to solve this problem

Any tips?

Ad

Answer

You can use the combineLatest() function.

Example:

combineLatest(observable1$, observable2$)
    .subscribe(([observable1, observable2]) => {
        console.log(observable1, observable2);
    });
Ad
source: stackoverflow.com
Ad