{"success":true,"name":"in-javascript-foreach-ignores-await-use-a-loop-that-actually","description":"Working in JavaScript and awaiting async work inside an array iteration","category":"ai-brimind","path":"ai-brimind/in-javascript-foreach-ignores-await-use-a-loop-that-actually/SKILL.md","file":"SKILL.md","content":"---\nname: in-javascript-foreach-ignores-await-use-a-loop-that-actually\ndescription: \"Working in JavaScript and awaiting async work inside an array iteration\"\n---\n\n# In JavaScript, forEach Ignores await: Use a Loop That Actually Waits\n\n## When to use\nProject is JavaScript/Node, and you are calling await inside forEach, map, or filter, or async loop results finish out of order or arrive as pending promises.\n\n## Example request\nawait inside forEach → forEach doesn't wait; the code after it runs before any item finishes. map with an async callback used directly → you get an array of promises, not values; missing Promise.all. 'save each, then read the total' → the read fires while saves are mid-flight; order illusion. — each trusts a loop that never awaited.\n\n## Instructions\nAct as a JavaScript mechanic who knows which loops honor await and which silently drop it. forEach cannot wait: it ignores the promises its callback returns, so the surrounding code continues immediately while the async work floats unfinished — the classic 'it saves but the next line sees nothing' bug. Before iterating with async work, decide the real requirement: one-after-another (order or resource limits matter) → use a for...of loop with await inside; all-in-parallel-then-continue → build the promises with map and await Promise.all, choosing the variant that matches your failure plan (all must succeed vs collect every outcome). Remember an async callback given to map or filter returns promises, not values — filter especially becomes meaningless, since every promise is truthy. When results are undefined, out of order, or '[object Promise]', suspect this pattern first. Only proceed once every async iteration states its choice — sequential for...of or awaited Promise.all — and nothing downstream runs before the work it depends on is confirmed done.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #724\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}