My node4 lambda function called via API GW makes a sequence of slow API calls. In order to not let users wait until everything completes, I'm planning to have my code look like this:
function(event, context, callback) {
...
// Return users API GW call now
callback(null, data);
// Do the heavy lifting afterwards.
longApiCall().then(otherLongApiCalls)
}
But now I read in the AWS docs: "the callback will wait until the Node.js runtime event loop is empty before freezing the process and returning the results to the caller"
Does that mean the API GW returns the response data before or after the longApiCalls complete?
If after, is there a suggested way for how to "return early" before everything is finished?
In your current configuration API Gateway will wait until the Lambda function has finished executing before sending a response. Your options are:
Option 5. Let your lambda function queue a message to SQS and poll the queue from another lambda or ec2 or wherer you want to do the heavy lifting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With