{"success":true,"name":"await-what-must-finish-parallelize-what-is-independent-async","description":"Writing async code with several awaits, or a promise fired without await","category":"ai-brimind","path":"ai-brimind/await-what-must-finish-parallelize-what-is-independent-async/SKILL.md","file":"SKILL.md","content":"---\nname: await-what-must-finish-parallelize-what-is-independent-async\ndescription: \"Writing async code with several awaits, or a promise fired without await\"\n---\n\n# Await What Must Finish, Parallelize What Is Independent: Async Order IS a Dependency Graph\n\n## When to use\nA function performs multiple async operations, and their ordering — what awaits what, what runs in parallel, what floats un-awaited — happened by habit rather than by mapping which step truly depends on which.\n\n## Example request\nawait createOrder() then await decrementStock() → correct: step two depends on step one existing; sequential await IS the right tool when there is a real dependency. await fetchClub() then await fetchCoaches() when neither needs the other → two round-trips paid in single file for nothing; independent operations belong in Promise.all, cutting the wait to the slowest one. sendEmail() called without await 'to be fast' → the route returns, and the unhandled rejection later crashes the process or vanishes silently; a floating promise is not background work, it is abandoned work — your move-slow-work-out discipline demands a catch/log at minimum. items.forEach(async ...) with awaits inside → forEach ignores promises entirely; the loop 'finishes' while nothing has run, and the code after it reads unwritten data — the classic trap, use for..of (sequential) or Promise.all(map) (parallel), chosen on purpose.\n\n## Instructions\nAct as a developer who reads every async function as a dependency graph, not a to-do list — each await is a declared edge saying 'this must finish before that starts', and the three classic async bugs are all graph errors: a missing edge (forgot await → later code reads data that does not exist yet), a fake edge (awaiting things sequentially that never depended on each other → the user pays double latency for your typing order), and an orphan node (a floating promise whose failure has nowhere to land → silent data loss or a process crash at a random later moment). For every multi-step async function, do this: (1) Before ordering the calls, say the real dependencies: which step consumes another step's RESULT or requires its SIDE EFFECT? 'Decrement stock' depends on 'order created'; 'fetch coaches' and 'fetch courts' depend on nothing but the input — write the graph in your head or a comment, then let the code mirror it exactly. (2) Give each edge its correct construct: true dependency → sequential await; independent operations → Promise.all([...]) so they run concurrently and the function waits for the slowest, not the sum — and when partial failure must not sink the batch, Promise.allSettled with explicit per-item handling, a deliberate choice per your handle-the-failure-path discipline, since Promise.all rejects wholesale on the first failure. (3) Hunt the orphans: any promise created without await, .then, or .catch is a floating promise — if the work is genuinely fire-and-forget (the email after the response, per your background-work discipline), attach the .catch that logs with intent and, where it matters, records the failure somewhere durable; 'fast because un-awaited' without a catch is not async design, it is error deletion. (4) Respect the loop trap as a special case of the graph: forEach cannot await — choose for..of when iterations depend on each other (or must not stampede the DB), Promise.all(items.map(...)) when they are independent — and remember your N+1 discipline outranks both: if the loop's body is a per-item query, the real fix is usually one batched operation, not better loop syntax. (5) Verify against the graph, not the happy log: make the slowest independent call artificially slow and confirm total time equals the slowest (parallel working), then make a floating promise reject and confirm it lands in a log rather than an unhandledRejection. Only call the function done when every await answers 'what depends on this?', every Promise.all answers 'why are these independent?', and no promise exits the function unowned — async code whose ordering came from typing order runs correctly only by coincidence, and coincidence has an expiry date under load.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #245\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}