繁体   English   中英

承诺中的角度承诺?

[英]Angular promise inside promise?

我有一个这样的功能:

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
    });

所以这里的问题是第一个 foreach 在第二个 foreach 之前结束,所以它解决了承诺,我怎么能等待第二个 foreach(在最新的 else if 中)在它结束第一个 foreach 之前完成?

您认为 promise 应该在 forEach 块中运行良好。 不,它不会以这种方式发生。 你必须确保你的内部承诺应该正确返回。 然后你可以创建一个 promises 数组,通过使用Promise.all你可以检查所有的 promises 是否完成。

代码

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