Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
How to Await a Loop of Async Operations?

I have a patch endpoint that takes a body of fields to update, iterates over each field, and updates the correct value. I want to do something send a response once all those things are done, but I do not know how to await a loop of operations. If it was a singular operation I could just add .then(), but that would not work in this case. What is an elegant solution to this?

Code:

const updateUser = (req, res) => {
    const db = mongoConnection.getDb();
    for (key in req.body) {
        db.collection('users').updateOne(
            {username: req.query.username},
            {$set: {[key]: req.body[key]}}
        )
    }
    // send response once for loop is done
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could mark the outer function async and await each DB update inside the loop. Then you'll know that after the loop completes, all DB updates are done.

An even better way is to run the updates in parallel since they do not depend on each other. You can use Promise.allSettled() which takes an array of promises and resolves when the last one is finished.

const updateUser = async (req, res) => {
  const db = mongoConnection.getDb();
  
  const dbUpdates = Object.entries(req.body).map((key, value) => {
    return db.collection('users').updateOne(
      { username: req.query.username },
      { $set: { [key]: value }}
    );
  });

  await Promise.allSettled(dbUpdates);

  // Send response, at this point all DB updates are done
};
about 4 years ago · Juan Pablo Isaza Relatório

0

I think an easy answer would be hiding the for loop in an async function, then use it in the endpoint.

const updateUserField = async () =>
  db.collection("users").updateOne(
    {
      username: req.query.username,
    },
    {
      $set: { [key]: req.body[key] },
    }
  );


const updateUserEndpoint = (req, res) => {
   for (let field of req.body) {
      await updateUserField(field);
   }

   res.status(200).send('updated');
}

Then, for testing, you could wrap the endpoint with a function that injects the function itself:

const updateUserEndpoint = (userUpdateFunc) => userUpdateFunc(req, res);

This pattern is called dependency injection - when writing unit tests for the endpoint, you'd just replace userUpdateFunc with whatever mocked function you want to use. This removes the need to monkeypatch the function in the test.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda