All courses

Unawaited async work and floating promises

Spot a missing `await`, a fire-and-forget call, `forEach` with an async callback, or a Python coroutine or Rust future that is created but never driven to completion. Explain the result in each language: work never runs, runs out of order, or fails silently.

Builds on: What awaiting actually does

▶ Watch this lesson Start the whole course

What the animation says

500 reported, 430 saved

You asked your coding agent to speed up a nightly import. It swapped the for loop for a forEach with an async callback. Tests went green. Its summary said rows are now saved concurrently. Next morning, the job reports five hundred rows imported. The table holds four hundred thirty. And the job's own try and catch logged nothing. Look at the new code. When does import rows return?

forEach drops the promises

It returns right away, while the saves are still running. forEach calls each callback, gets back a promise, and throws it away. So import rows returns, the job reports five hundred, closes the database, and exits. Saves still in flight at that moment never land. A save that fails rejects a promise nobody holds, so the job's catch never sees it. Node may only report an unhandled rejection. Tap the saves that get cut off.

Why it passed, and the fix

The agent wrote this because it looks like the old loop. The type checker accepts it. Tests stay green because the in-memory test database saves almost instantly. A lint rule, no floating promises, flags it. The compiler does not. The fix: give every promise an owner. Map each row to its save, and collect them with Promise all. If the order of saves matters, just keep the old loop. Drag the await to the spot that makes import rows wait.

Python: the same tell

Now the same mistake in Python. Inside an async checkout function, the agent calls notify order, with no await in front. Notify is also an async function. In JavaScript, a floating promise still runs, just without an owner. So what does this Python line do?

Every async call needs an owner

Nothing is sent. The call only builds a coroutine, and Python warns that it was never awaited. Rust is the same. Binding a future to let underscore drops it unrun, and hides the warning. The fix: await notify in Python. In Rust, add dot await and handle the result. The rule: every promise, coroutine, or future needs an owner. Await it, return it, collect it, or store it and await it later. A spawned task whose handle is dropped does run, but its result and errors are lost. Tap the two lines here with no owner.

500 rows imported, 430 in the table

You asked the agent to speed up a nightly import. It replaced the `for` loop with `rows.forEach(async (row) => …)`. The tests went green, and its summary said *rows are now saved concurrently*. Next morning the job reports 500 rows imported, but the table holds 430, and the job's own `try/catch` logged nothing. Where did 70 rows go, and why did nothing catch the failure?

The await is there, in the wrong place

`forEach` calls each callback and throws away what it returns, here a promise. So `importRows` returns, and the job reports 500, while saves are still running. Saves still in flight when the job closes its database connection and exits are lost. A save that fails rejects a promise nobody holds, so the caller's `catch` never sees it. At most, Node reports an unhandled rejection outside the job's logging. The agent writes this because it looks like the old loop, the type checker accepts it, and tests stay green when the test's in-memory database saves almost instantly. **Later cost:** lost writes, effects out of order, errors far from their cause.

The tells to scan for

Look for an async call whose result is not awaited, not returned, not handed to `Promise.all` or a task group, and not stored and awaited later. - **JS/TS:** a bare `save(x);`, `forEach(async …)`, `.map(async …)` whose promises never reach `Promise.all`, or a `try` around an unawaited call. typescript-eslint's `no-floating-promises` and `no-misused-promises` flag most of these; `tsc` does not. - **Python:** an `async def` called without `await`, or a `create_task(…)` result thrown away. - **Rust:** a future dropped or bound to `let _`, or a spawned task's handle dropped.

Python and Rust: the work never starts

In Python the same tell has a harsher result. The agent writes `notify(order)` inside an `async def`. Because `notify` is also `async def`, this only builds a coroutine. No email goes out, and the only trace is `RuntimeWarning: coroutine 'notify' was never awaited`. In Rust, `let _ = audit.record(ev);` builds a future and drops it. Nothing is written, and `let _` silences the `must_use` warning. **BETTER:** `await notify(order)` in Python; in Rust, `audit.record(ev).await` and handle its result.

The diff behind the missing rows

The tell is `forEach(async` on the `+` side. The old loop awaited each save. Now `importRows` waits for none of them, so `return rows.length` counts rows that may never land. **BETTER, concurrent:** `await Promise.all(rows.map((r) => db.save(r)));`. It waits for all the saves and rejects if any of them fails. **BETTER, sequential:** keep the old loop.
// TypeScript
--- a/src/import.ts
+++ b/src/import.ts
 async function importRows(rows: Row[]) {
-  for (const row of rows) {
-    await db.save(row);
-  }
+  rows.forEach(async (row) => {
+    await db.save(row);
+  });
   return rows.length;
 }

Every async call needs an owner

Each promise, coroutine or future needs an owner. It must be awaited, returned, collected with `Promise.all` or a task group, or stored and awaited later. - A bare coroutine (Python) or future (Rust) with no owner never runs. - In JS/TS it runs, but unordered and outside the caller's `catch`. - A spawned task whose handle is dropped runs, but its result and errors are lost. Scan diffs for a missing `await`, `forEach(async`, `.map(async` without `Promise.all`, `let _ =`, and dropped task handles.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.