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?


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.

Leave a Reply