Making sure observables in for loop are all finished before executing other code

后端 未结 3 972
醉话见心
醉话见心 2020-12-02 03:08

I have a piece of code that looks like this:

getPersons().subscribe(
    persons => {
        for (const person of persons) {
            getAddress(perso         


        
相关标签:
3条回答
  • 2020-12-02 03:28

    You should make use of RxJS's forkJoin to wait for the for..of loop to be completed before returning all the observables.

    Here are the changes you should make to your code:

    getPersons().subscribe(
      persons => {
        const observablesList = [];
        for (const person of persons) {
          const getAddressObservable = getAddress(person.id);
          observablesList.push(getAddressObservable)
        }
        forkJoin(observablesList).subscribe(response => {
          // console.log(response) to check that there is a list of returned observables
          const result = persons.map((person, index) => {
            person['address'] = response[index]['address'];
            return person;
          })
          doSomethingWithAddresses();
        })
      }
    );
    

    Alternatively, you may try this to prevent the chaining of subscribe()

    getPersons().pipe(
      mergeMap(persons => {
        const observablesList = [];
        for (const person of persons) {
          const getAddressObservable = getAddress(person.id);
          observablesList.push(getAddressObservable)
        }
        return observablesList;
      })
    ).subscribe(response => {
      // console.log(response) to check that there is a list of returned observables
      const result = persons.map((person, index) => {
        person['address'] = response[index]['address'];
        return person;
      })
      doSomethingWithAddresses();
    })
    
    0 讨论(0)
  • 2020-12-02 03:38
    let loadedPerson;
    getPersons().pipe(
      mergeMap(persons => {
      return of(persons);
    }),
    mergeMap(person => {
      loadedPerson = person;
      return getAddresses(person.id);
    }),
    map((address) => {
      loadedPerson.address = address;
    }),
    tap(()=>{
      doSomethingWithAddresses();
    })
    ).subscribe();
    
    0 讨论(0)
  • 2020-12-02 03:41

    Try this approach

    methodOne() {
    getPersons().subscribe(
        persons => {
            for (const person of persons) {
                getAddress(person.id).subscribe(
                    address => {
                        person.address = address;
                    }
                );
            }
         }
    );
    }
    
    async methodTwo() {
    await methodOne();
    doSomethingWithAddresses();
    }
    
    0 讨论(0)
提交回复
热议问题