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

后端 未结 3 996
醉话见心
醉话见心 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: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();
    }
    

提交回复
热议问题