---
title: "Why Does My JavaScript Function Finish Before Its async forEach Callbacks?"  
description: "Why Does My JavaScript Function Finish Before Its async forEach Callbacks?"  
author: "Stefano Coppola"  
published: 2026-10-19  
canonical: https://answers.mindstick.com/qa/117886/why-does-my-javascript-function-finish-before-its-async-foreach-callbacks  
category: "Mix Category"  
tags: ["javascript", "Async Await", "Debugging"]  
reading_time: 1 minute  

---

# Why Does My JavaScript Function Finish Before Its async forEach Callbacks?

I have a `saveRow()` helper that returns a promise for a database write. I want `saveAll()` to finish only after every row has been saved, but the completion message appears while writes are still running.

```js
async function saveAll(rows) {
    // Attempt to wait for all row-saving callbacks.
    await rows.forEach(async (row) => {
        // Each callback waits for its own database write.
        await saveRow(row);
    });

    console.log("All rows saved");
}

// The caller also needs to wait for the writes.
await saveAll(rows);
```

This runs in a JavaScript module, and both `rows` and `saveRow` are defined elsewhere. The database helper behaves as expected when I await a single write.

**What am I misunderstanding about async callbacks in forEach?** Does the outer `await` wait for anything here?

For this import, rows should be saved in order, and a failed write should stop the remaining rows. I’m looking for the appropriate loop structure for those requirements, rather than simply starting every write at once.


---

Original Source: https://answers.mindstick.com/qa/117886/why-does-my-javascript-function-finish-before-its-async-foreach-callbacks

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
