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
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.
The await is there, in the wrong place
The tells to scan for
Python and Rust: the work never starts
The diff behind the missing rows
// 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
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.
Bytewise
All courses