等待异步forEach



我需要停止我的函数myFunction()来处理,如果在名为this.constraint.costs的jsonArray中找到零值。

这是我的第一个函数:
myFunction(){
if(this.zeroValueCosts()){
console.log(" Found an integer 0 value, I need to stop my function")
return;
};
}

这是我的第二个async函数:

async zeroValueCosts(){
await this.constraint.costs.forEach(function(cost){
if(cost.value = 0){
return true;
}
else{
return false;
}
});
}

不工作,没有错误显示

使用async/await和forEach循环的正确方法是什么?

This .constraints.costs数组:

costs: [{
"name": "new cost",
"value": 0.06666
},{
"name": "new cost 2",
"value": 0.05666
}]

你甚至不需要async await

async myFunction(){
if(this.zeroValueCosts()){
return;
};
}
zeroValueCosts(){
return constraint.costs.some(({ value }) => value === 0);
}

最新更新