I am using AWS lambda for my application to get multiple records from dynamo using promises. Initially I set my context.callbackWaitsForEmptyEventLoop = true, so the handler will wait until the promises are resolved. Once the promises are resolved I am setting context.callbackWaitsForEmptyEventLoop = false and then calling callback(null);
If I don't set the callbackWaitsForEmptyEventLoop = false after the promises are resolved, the function is timing out even after I call callback(null)(some libraries functions might be still in the event queue).
So, the problem is if same the runtime is used again for next-time, the previously left over functions in the event queue are getting resumed, rather than starting all over again. Is there a way around this or even empty the previous event queue?
context.callbackWaitsForEmptyEventLoop = true;
allPromises = getDatafromDynamo();
allPromises.then(results => {
console.log('Results are', JSON.stringify(results));
context.callbackWaitsForEmptyEventLoop = false;
callback(null, 'DONE');
return;
}).catch((err) => {
console.log("Error occurred", err);
context.callbackWaitsForEmptyEventLoop = false;
callback(err);
});