Strange async function behavior

Hi all,

in my javascript service, I’ve an async function that should write a log in a table when finished execution, both on then and catch block. i.e.

//insert "INIT" record on backendless table

myAsyncFunction()
.then((value) => {
    // insert "Success End" record on backendless table
})
.catch((error) => {
    // insert "Error End" record on backendless table  
});

if I debug with coderunner on VSCode, both initial record and the one in then block are inserted

if I run the service without debugging it, only initial record is inserted, not the one in the then or catch block

Hi, @Elena_Aralla

Please take a look at this doc. I think it may be useful for your case: Troubleshooting - Developing Backendless Server Code with node.js

Regards,
Marina

Hi @Marina.Kan,

I read the doc you suggest and done some check; these are the result:

If I run coderunner on VS Code and then call the API from backend, nothing was written in files->logging-> but in debug console of VSCode I read:

19:01:07.070 [master] [C48B29B2-B924-1801-FFCA-A378FC218700] New task arrived!
node_modules/backendless-coderunner/lib/util/logger.js:110
19:01:07.071 [master] [C48B29B2-B924-1801-FFCA-A378FC218700] [INVOKE SERVICE] services.TestService.HelloWorldAsync
node_modules/backendless-coderunner/lib/util/logger.js:110
19:01:07.071 [master] console.log - HelloWorldASync - INIT
node_modules/backendless-coderunner/lib/util/logger.js:110
19:01:07.074 [master] console.log - HelloWorldASync - END
node_modules/backendless-coderunner/lib/util/logger.js:110
19:01:07.075 [master] [C48B29B2-B924-1801-FFCA-A378FC218700] Processing finished
node_modules/backendless-coderunner/lib/util/logger.js:110
19:01:12.077 [master] console.log - HelloWorldASync - The Promise is resolved; it return: done!

if I call the api from backend without coderunner on VSCode, the log file is written and this is it’s content:

19:2:37.388 | SERVER_CODE | INFO | [5345] Building ServerCode Model for path (/opt/backendless/repo/853476f7-1565-cba3-ff64-81d8a9303300/files/servercode/JS/default/PRODUCTION)
19:2:37.389 | SERVER_CODE | INFO | [5345] ServerCode Model built in 5ms
19:2:37.390 | SERVER_CODE | INFO | [5345] Reading services/TestService.js...
19:2:37.391 | SERVER_CODE | INFO | [5345] ServerCode Model extended in 2ms
19:2:37.393 | SERVER_CODE | INFO | [5345] [60650598-B921-A0B7-FF68-4277D7C0EE00] [INVOKE SERVICE] services.TestService.HelloWorldAsync
19:2:37.396 | SERVER_CODE | INFO | [5345] console.log - HelloWorldASync - INIT
19:2:37.396 | TestService | INFO | logger.info - HelloWorldASync - INIT
19:2:37.397 | TestService | INFO | start
19:2:37.397 | SERVER_CODE | INFO | [5345] console.log - HelloWorldASync - END
19:2:37.397 | TestService | INFO | logger.info - HelloWorldASync - END
19:2:37.398 | SERVER_CODE | INFO | [5345] Processing finished in 19.823ms
19:2:37.398 | SERVER_CODE | INFO | [5345] Task results sent
19:2:37.398 | SERVER_CODE | INFO | [5345] Flushing logs

in the debug console of VS Code I can see the log:

console.log - HelloWorldASync - The Promise is resolved; it return: done!

that I can’t find in log file!

In the second case, I don’t know if my async function (MyFunction) finished with success or not! :frowning:

Thank you,
Elena.

Hello @Elena_Aralla

That was caused because your code doesn’t wait for resolving myAsyncFunction and the process was killed before you resolved your promise. When you use local debug it works because the process is not stopped.

https://backendless.com/docs/bl-js/bl_troubleshooting.html#servercode-works-well-in-the-debug-mode-but-does-not-when-deployed-to-the-cloud

Try to rewrite your logic with await for your async function. Or return a promise where your end log will be in finally construction.

Regards, Dima