An async function passed to forEach runs without anyone awaiting its promise

tx0/ts:async-foreach-callbackdefaultwarningbug

What it finds

items.forEach(async (item) => { await save(item); }): forEach ignores what its callback returns, so the promises of an async callback are dropped. The loop does not wait for them, the code after it runs before they finish, and their rejections are not handled anywhere.

for (const item of items) with await runs them in turn, and Promise.all with map runs them together and waits for all.

From the rule's source, async_foreach_callback.rs.

Example from the rule's tests
async function saveAll(items: string[]) {  items.forEach(async (item) => {tx0 reports: forEach does not wait for this async callback    await save(item);  });  items.forEach(async function (item) {tx0 reports: forEach does not wait for this async callback    await save(item);  });  items.forEach((item) => {    void save(item);  });  for (const item of items) {    await save(item);  }}async function save(item: string) {  return item;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:async-foreach-callback" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:async-foreach-callback] reason="..."

tx0 explain tx0/ts:async-foreach-callback shows the same in the terminal. Found a false positive? Report it.