Javascript ForOf循环不等待异步等待调用

Javascript ForOf循环不等待异步等待调用,javascript,async-await,Javascript,Async Await,我希望我的循环在开始评估新的performAction之前,等待两个异步调用performAction和checkAssertion。我读到我应该使用for of循环,但从日志中我得到了如下信息: 1. action which violates the assertion 2. action from another bot 3. found and assertion violated during checkAssertion 4. found and assertion violated

我希望我的循环在开始评估新的
performAction
之前,等待两个异步调用
performAction
checkAssertion
。我读到我应该使用for of循环,但从日志中我得到了如下信息:

1. action which violates the assertion
2. action from another bot
3. found and assertion violated during checkAssertion
4. found and assertion violated during checkAssertion
如何强制for循环等待

   public async start(duration: number) : Promise<void> {

    // init
    await this.updateStatus();

    // for each round, let bots perform actions
    let bots = this._bots;
    for (let currentStep = 0; currentStep < duration; currentStep++){

        Logger.info({"label": "step", "stepNumber": currentStep});
        console.log("Step: " + currentStep);

        for (const bot of bots) {
            try {
                let transactionResult = await bot.performAction();
                await this.checkAssertion(transactionResult);
            } catch (error) {
                let err = (error as Error);
                // handle error here
            }                
        }
    }
}
public异步启动(持续时间:number):承诺{
//初始化
等待这个;
//对于每一轮,让机器人执行动作
让机器人=这个;
对于(让currentStep=0;currentStep
编辑:

受保护的异步checkAssertion(transactionResult:any):承诺{
if(transactionResult!=null){
this.checkTotalBalance().then(结果=>{
断言(结果,“总余额不等于总供给”);
断言(!this.checkSuccessfulOverflow(transactionResult),“使用溢出成功执行事务”);
}).catch(异步(错误)=>{
info({“label”:“assertionFail”,“error”:error});
等待这个;
投掷误差;
});
}
}

我认为这是因为您在函数
checkAssertion
中使用了
then(…)
catch(…)
(而不是
try…catch
)。因此,它在所有
performation()调用完成后执行其操作。因此,将此函数重写如下:

protected async checkAssertion(transactionResult: any) : Promise<void> {
  if (transactionResult != null) {
    try {
      const result = await this.checkTotalBalance();
      assert(result, "Total balance not equal to totalSupply");
      assert(!this.checkSuccessfulOverflow(transactionResult), "Successful transaction execution with overflow");
    } catch (error) {
      Logger.info({ "label": "assertionFail", "error": error });
      await this.updateStatus();
      throw error;
    }
  }
}
受保护的异步checkAssertion(transactionResult:any):承诺{
if(transactionResult!=null){
试一试{
const result=等待此值。checkTotalBalance();
断言(结果,“总余额不等于总供给”);
断言(!this.checkSuccessfulOverflow(transactionResult),“使用溢出成功执行事务”);
}捕获(错误){
info({“label”:“assertionFail”,“error”:error});
等待这个;
投掷误差;
}
}
}

看起来不错。您确定这些函数返回了正确的承诺吗?@Bergi我想是的,它们都声明为
async
performAction
返回一个
Promise
并且
checkAssertion
不返回任何内容,因此
Promise
。也许我把最后一个弄得一团糟(我编辑了上面的代码,添加了函数的代码)。我仍在学习承诺/异步等待。:)是的,您忘记了
返回
任何内容:-)另外,如果您想使用
异步
/
等待
,我建议不要使用
,然后
对不起,那么我需要在函数末尾添加一个空的
return
语句吗?@Bergi我认为当您使用异步函数时,您不应该显式返回承诺对象,它会自动返回-否?
protected async checkAssertion(transactionResult: any) : Promise<void> {
  if (transactionResult != null) {
    try {
      const result = await this.checkTotalBalance();
      assert(result, "Total balance not equal to totalSupply");
      assert(!this.checkSuccessfulOverflow(transactionResult), "Successful transaction execution with overflow");
    } catch (error) {
      Logger.info({ "label": "assertionFail", "error": error });
      await this.updateStatus();
      throw error;
    }
  }
}