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

652
Visualizações
AWS Lambda does not execute the promises without await

I have the following handler for my AWS Lambda function. I don't want to or need to wait for Promise.all resolve or reject. Because, I am not interested about its result. My purpose is to just send those requests and finish. The problem is that if I remove the await keyword (pointed with an arrow), the requests are not firing up. But, if I put the await keyword, the Lambda function needs to run and be charged for 3-5 seconds, and it is not cost effective for me.

Could you explain what I am doing wrong? Why the requests are not being sent without the await keyword?

module.exports.handler = async (event) => {
  try {
    const urls = [/*some API urls*/];
--> await Promise.all(urls.map((url) => {
      return axios.post(url, { email })
        .catch((error) => {
          console.log(error);
        });
    }));

    return {
      statusCode: 200,
      body: JSON.stringify({ message: 'OK' }),
      headers: {
        'Content-Type': 'application/json',
      },
    };
  } catch (error) {
    return {
      statusCode: 500,
      body: JSON.stringify({ message: 'INTERNAL_ERROR' }),
      headers: {
        'Content-Type': 'application/json',
      }
    }
  }
};
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

As stated in the docs

If your code performs an asynchronous task, return a promise to make sure that it finishes running. When you resolve or reject the promise, Lambda sends the response or error to the invoker.

If you don't return a promise, there is no guarantee your async tasks will run to an end. With making the handler async you are indeed returning a promise. But without awaiting the Promise.all this promise from the handler is immediately resolved, and thus execution of your lambda (and all unfinished async tasks) is stopped.

And this seems quite logical. Otherwise everybody would write the heavy lifting computations in an async task which runs for a long time. And the only synchronous call in the lambda function would be calling that async task and the lambda function would return after (and be charged for) only a few milliseconds.

If you want to pay only for a 100ms, make your task fast enough. If you task takes 4 seconds, you have to pay for 4 seconds.

over 4 years ago · Santiago Trujillo 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