Home > OS >  Wait for async forEach
Wait for async forEach

Time:10-26

I need to stop my function myFunction() to process, if a zero value gets found in a jsonArray called this.constraint.costs.

this is my 1st function :

myFunction(){
  if(this.zeroValueCosts()){
      console.log(" Found an integer 0 value, I need to stop my function")
      return;
  };
}

this is my second supposed async function :

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

It doesn't work, no error displayed

What is the right way to use async/await with a forEach loop result ?

This is the this.constraints.costs array :

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

CodePudding user response:

Well, you dont even need async await

async myFunction(){
  if(this.zeroValueCosts()){
      return;
  };
}

zeroValueCosts(){
   return constraint.costs.some(({ value }) => value === 0);
}

CodePudding user response:

Thanks a lot Ifaruki, I have refreshed the app, and it works now !

  • Related