Hi guys,
I’m stuck on an issue that I can’t wrap my brain around.
ISSUE:
When invoking a function locally, the code does not wait until callbacks are hit before execution finishes…
STEPS TO REPRODUCE:
Create any new project from a serverless template. E.g.
serverless create --template aws-nodejs
Add a function with a callback to your serverless function. E.g.
console.log(‘hello start’);
setTimeout(function() {
console.log(‘timeout hit!’); // This line is never reached…
}, 2000);
console.log(‘hello end’);
CONCLUSION:
The serverless ‘invoke local’ function finishes and the process exits after the ‘hello end’ is reached but doesn’t wait for the setTimeout callback. So I assume it is exiting the process even though something is still in the event loop?
Please could it be clarified why this occurs only with ‘invoke local’ (‘invoke’ works fine once deployed to AWS), if it’s a bug and if there is a fix/workaround? It makes it very difficult for testing anything callback/promise based, e.g. ‘node-fetch’ npm library, without having to deploy each time…
Please could you also provide a working example if it is fixable.
Many thanks to all,
Eckseller