我希望我的循环在开始评估新的performAction
并继续其循环之前,先等待两个checkAssertion
和performAction
异步调用。我读到我应该使用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-loop等待?
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
}
}
}
}
编辑:
protected async checkAssertion(transactionResult: any) : Promise<void> {
if (transactionResult != null) {
this.checkTotalBalance().then(result => {
assert(result, "Total balance not equal to totalSupply");
assert(!this.checkSuccessfulOverflow(transactionResult), "Successful transaction execution with overflow");
}).catch( async (error) => {
Logger.info({"label": "assertionFail", "error": error});
await this.updateStatus();
throw error;
});
}
}
答案 0 :(得分:0)
我认为这是因为您在函数then(...)
(而不是catch(...)
)中使用了checkAssertion
和try ... catch
。因此,它将在所有performAction()
个调用完成后 进行操作。因此,请按如下所示重写此函数:
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;
}
}
}