JavaScript
Await work sequentially with for...of
Use a for...of loop with await when tasks must complete one at a time. An async callback passed to forEach does not make the surrounding function wait for those callbacks.
Worked example
Node.js example · save as example.mjs and run node example.mjs
const visited = [];
async function save(value) { visited.push(value); }
for (const value of [1, 2, 3]) {
await save(value);
}
console.log(JSON.stringify(visited));How it works
Each loop iteration waits before the next iteration begins. This preserves a dependency chain and makes ordinary try/catch control flow work as expected. It is appropriate when the second operation relies on the first or a service requires serialized requests.
Limits and mistakes
Sequential execution can be unnecessarily slow for independent operations. It also does not supply a delay between requests beyond the task’s own duration. Choose concurrency according to dependencies and service limits, rather than treating one loop style as universally faster.
Check the result
Expect [1,2,3]. Add different delays inside save and confirm the order remains stable. Then decide whether the actual tasks need this ordering or can safely overlap.
Sources
Reference documentation for the behavior used in this guide: