我有这样的功能:
df %>%
group_by(id) %>%
mutate(new.id = case_when(
all(values < 1000) ~ "low/low",
all(values > 1000) ~ "high/high",
TRUE ~ "low/high"
))
所以这里的问题是第一个foreach在第二个foreach之前结束,因此它解决了诺言,我如何才能等待第二个foreach(在最新的else中)在它结束第一个foreach之前完成?
答案 0 :(得分:0)
您正在考虑诺言应该在forEach块中很好地发挥作用。不,它不会以这种方式发生。您必须确保您的内在承诺应被正确退还。然后,您可以创建一个promise数组,并使用Promise.all
可以检查是否所有的promise都已完成。
代码
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);
});