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

242
Visualizações
Is not awaiting certain promises wrong?

Using express I have one REST route where I fetch some data. Within this request I also add an entry into an event table (for app insights purposes) in the database. I do not really care to wait for that event to be saved before returning the actual data to the requester, since saving the event is not that important for the end user.

Something like this pseudo code:

const data = await getResponseDataFromDatabase();

insertInsightsEventIntoDatabase(); // returns a promise which I don't want to wait for

return data;

Since there was a bug in the insertInsightsEventIntoDatabase I got this error in the logs:

This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
Error: Cannot set headers after they are sent to the client

If I await the insertInsightsEventIntoDatabase() I would get a much better error log in the console that shows an error from the ORM I am using.

My question is, it it wrong to not await the method? I like the idea of not awaiting it just to have it "run in the background" and not blocking the time it takes to send the response on the client request. The drawback is that the real error seems to be swallowed and I would very much like to have the original error in the logs.

Am I doing this wrong?

about 4 years ago · Santiago Gelvez
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