I have a simple lambda function that asynchronously makes an API calls and then returns data. 99% of the time this works great. When ever the API takes longer then the lambda co
You should look for how your function handle works with a specific
context.callbackWaitsForEmptyEventLoop
If that boolean-type is false
, the setTimeout won't be ever fired, because you might've answered/handled the lambda invocation earlier.
But if the value of callbackWaitsForEmptyEventLoop
is true
- then your code will do what you are looking for.
Also - it's probably easier to handle everything via callbacks directly, without the need for "hand-written" timeouts, changing configuration timeouts and so on...
E.g.
function doneFactory(cb) { // closure factory returning a callback function which knows about res (response)
return function(err, res) {
if (err) {
return cb(JSON.stringify(err));
}
return cb(null, res);
};
}
// you're going to call this Lambda function from your code
exports.handle = function(event, context, handleCallback) {
// allows for using callbacks as finish/error-handlers
context.callbackWaitsForEmptyEventLoop = false;
doSomeAsyncWork(event, context, doneFactory(handleCallback));
};