Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
How do I make sure that async operations finish before sending a response in Express.js?

I am trying to:

  1. Retrieve an array of objects containing account information from my Database,
  2. Use the account_id from those accounts to make a fetch request to an external API for each of the accounts and retrieve transactions
  3. Loop over every single transaction in each account and save it one-by-one into my database.
  4. Send the newly-saved transactions back to the frontend as a response to the API call

Everything works great, except that the last part of my code that is outside of the two loops runs before the async tasks finish.

My question is - how do I make sure that the last part of the code (console.log and the DB query that retrieves the newly-saved transactions) executes after all the async tasks finish?

I was trying to use callbacks / promises and follow other guides, but I did not manage to get it to work, so I would be grateful for some help!

I have the following Express.js code:

router.post("accounts/transactions", async (req, res) => {
  //user id sent from frontend
  const user_id = req.body.user_id;
  //Find accounts by user_id in DB
  const accounts = Account.find({ user_id: user_id }).then((accounts) => {
    return accounts;
  });
  //Once accounts retrieved from DB, loop over them and fetch transactions for each account
  (await accounts).forEach(async (account) => {
    const url = `https://someExternalApi.whatever/${account.accountId}/transactions`;
    const options = {
     //fetch request options
    };

    const response = await fetch(url, options)
      .then((res) => res.json())
      .catch((e) => {
        console.error({
          message: "Error!",
          error: e,
        });
      });

    //Once transactions for a single account are retrieved from external API, 
    //loop over them and save them one-by-one to DB

    (await response).transactions.booked.forEach(async (transaction) => {
      const filter = {
        transactionId:
          transaction.transactionId
      };
      const mongooseOptions = { upsert: true, new: true };
      const update = {
        user_id: user_id,
        //...other keys-value pairs for transaction record
        updated: Date.now(),
      };

      //Save single transaction to DB
      Transaction.findOneAndUpdate(filter, update, mongooseOptions)
        .then(
          console.log(
            `Transaction ${
              transaction.transactionId
            } saved!`
          )
        )
        .catch((error) => console.log(error));
    });
  });
  //The problem is that the below runs before the above async operations are finished
  console.log("All transactions updated!");
  Transaction.find({ user_id: user_id }).then((transactions) => {
    res.json(transactions);
  });
});

Thanks!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!