{"success":true,"name":"control-concurrency-when-looping-async-work-in-node-js","description":"Node.js loop running awaits or firing many promises at once.","category":"ai-brimind","path":"ai-brimind/control-concurrency-when-looping-async-work-in-node-js/SKILL.md","file":"SKILL.md","content":"---\nname: control-concurrency-when-looping-async-work-in-node-js\ndescription: \"Node.js loop running awaits or firing many promises at once.\"\n---\n\n# Control Concurrency When Looping Async Work in Node.js\n\n## When to use\nProject is Node.js, and you are writing a loop over async operations (API calls, DB queries, file ops) for a list of items.\n\n## Example request\nawait inside a for loop for 500 independent items → fully serial, 500x slower than needed — they could run concurrently. items.map(async …) with Promise.all on 10,000 items → all fire at once; APIs rate-limit and pools exhaust — bounded concurrency (p-limit) is the pattern. forEach(async …) → forEach ignores promises; the code continues before any finish — this is the fire-and-forget trap. One failure in Promise.all kills all results → Promise.allSettled when partial success is fine.\n\n## Instructions\nAct as a Node.js developer who decides the concurrency model BEFORE writing any async loop. Classify the work first: (1) steps depend on each other → sequential for...of with await is correct, say so; (2) items are independent and few (<~20) → Promise.all over a map; (3) items are independent and many, or hit rate-limited APIs / DB pools → bounded concurrency with an existing tool (p-limit, p-map) — never unbounded Promise.all on large lists, and never invent a custom semaphore when these packages exist. Know the traps: forEach ignores async callbacks entirely (silent fire-and-forget), and map without awaiting the promises does the same. Then choose the failure policy explicitly: Promise.all when one failure should abort, Promise.allSettled when partial results are acceptable — and handle the rejected entries either way. If you don't know the list size or the downstream rate limits, find out before choosing. Only write the loop once concurrency bound and failure policy are both confirmed, and state why they fit the data size and the downstream service.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #993\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}