简体   繁体   中英

Correct way to use async function inside a for-loop

Recently, I got some error when running the E2E tests and the only change I made is adding the checkColumns logic in the original test code as following:

it('check search and sort', async () => {
  await checkLoadingAndResult();
  await checkColumns(table, ...columns); //newly added 
  await checkTableSorting();
});

The logic of checkColumns is like:

export async function checkColumns(table: Table, ...columns: string[]) {
  for (const col of columns) {
    expect(await table.isColumnDisplayed(col)).toBeTruthy(`${col} is not displayed`)
  }
}

The error message is like:

Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:59536

I think maybe there's something wrong in the checkColumns function and I don't know whether it's a correct way to call async methods inside a for-loop. And I guess this for-loop is the cause of the error.

This error message is not generated by your usage of async / await. It is most likely displayed because the HTTP request sent failed due to a connection error, I had the issue on another framework and it was due to the webdriver that was not running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM