简体   繁体   English

承诺中的角度承诺?

[英]Angular promise inside promise?

I have a function like this:我有一个这样的功能:

resulta = 0;
resultb = 0;
resultc = 0;
resultd = 0;
    
    var wait = new promise((resolve, reject) => {
        data.forEach((single,index,array) =>{
            if (single.a == '1'){this.resulta = '1'}
            else if (single.a=='2') {this.resultb = '2'}
            else if (single.a=='3'){this.resultc = '3'}
            else if (single.a=='4'){
                this.function(single.a).then(async (bs) => {
                    if (somecondition) {
                        await bs.forEach(b => {
                            this.ccount = 0;
                            if(condition === true){
                                this.c = 'yes';
                                this.ccount = this.ccount +1;                      
                            }
                        });
                    }
                    if(this.ccount > 0){
                        this.resultd = '4';
                    }
                    else {
                       this.resultd = 'N/A';
                    }
                }
            }
            if (index === array.length -1)resolve();
        });
    });
    wait.then(async()=>{
        console.log('count', this.count); // always is 0 but it should be something > 0
        console.log('resulta', this.resulta); // 1 as it should be
        console.log('resultb', this.resultb); // 2 as it should be
        console.log('resultc', this.resultc); // 3 as it should be
        console.log('resultd', this.resultd); // 0 and it should be or 4 or N/A
    });

so the problem here is that the first foreach ends before the 2nd foreach so its resolve the promise, how can i wait for the second foreach (inside the latest else if) to finish before it ends the first foreach?所以这里的问题是第一个 foreach 在第二个 foreach 之前结束,所以它解决了承诺,我怎么能等待第二个 foreach(在最新的 else if 中)在它结束第一个 foreach 之前完成?

You're thinking promises should work well in forEach block.您认为 promise 应该在 forEach 块中运行良好。 No it does not happen in that way.不,它不会以这种方式发生。 You have to make sure that your inner promise should be return returned properly.你必须确保你的内部承诺应该正确返回。 Then you can create a promises array, and by using Promise.all you can check whether all promises are finished or not.然后你可以创建一个 promises 数组,通过使用Promise.all你可以检查所有的 promises 是否完成。

Code代码

const promises = data.map((single,index,array) =>{
    if (single.a == '1'){ return this.resulta = '1' }
    else if (single.a=='2') {return this.resultb = '2' }
    else if (single.a=='3'){ return this.resultc = '3' }
    else if (single.a=='4') {
        return this.function(single.a).then((bs) => {
            if (somecondition) {
                bs.forEach(b => {
                    this.ccount = 0;
                    if(condition === true){
                        this.c = 'yes';
                        this.ccount = this.ccount +1;                      
                    }
                });
            }
            if(this.ccount > 0){
                this.resultd = '4';
            }
            else {
               this.resultd = 'N/A';
            }
        }
    }
});

Promise.all(promises).then(async()=>{
    console.log('count', this.count);
    console.log('resulta', this.resulta);
    console.log('resultb', this.resultb);
    console.log('resultc', this.resultc);
    console.log('resultd', this.resultd);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM