Serverless doesn't wait for callback -> serverless invoke local -f <Func>

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

1 Like

I have same issue. It happened after updating from version 1.31 to 1.33 and I have it on last veriosn(1.34) too. I hope they fix it soon.