Your case looks oddly similar to:
Even they are getting 3 attempts and ending up with a 422. I’ll be curious to know if it’s affecting multiple users.
AWS Lambda functions need to be carefully written to strictly be async
. This is because, unlike browsers where the front-end will eventually get a response (as long as the tab is open), AWS terminates the function as soon as it hits the return
statement. So, if you don’t wait for something to complete before firing off the final return
statement, that task would never complete. This is where Promises come into play.
The way a Promise (prefixed with a return
keyword) would work (in this context) is: The function would wait for a piece of code to finish executing before moving on to the next line, and in your case, firing the final return
statement.The Promise is considered executed when it returns resolve()
or reject()
(in case of success/failure respectively). So, here, we’re calling resolve()
after the https
connection is closed and thus, we’re making sure that the request is fully done before moving on to send the data back to the client.
About why it makes a call thrice, I’ve absolutely no idea. You’re right, it should not, yet it does. The only thing you could try right now is that, does this also happen when using node-fetch
or axios
? You probably not want to add an extra dependency to your project, but I guess it’s worth a try, right?