Node.js: will node wait for setTimeout () to complete before exiting?

Consider:

node -e "setTimeout(function() {console.log('abc'); }, 2000);" 

This will cause the timeout to begin before the program exits.

Basically I wonder if this means that node is designed to wait for all timeouts to complete before exiting.

Here is my situation. My client has a node.js server, which it starts from Windows with a shortcut. If a node application encounters an exceptional condition, it usually exits instantly, without leaving enough time to see what the error is in the console, and this is bad.

My approach is to wrap the entire program with try catch, so now it looks like this: try { (function () { ... })(); } catch (e) { console.log("EXCEPTION CAUGHT:", e); } try { (function () { ... })(); } catch (e) { console.log("EXCEPTION CAUGHT:", e); } try { (function () { ... })(); } catch (e) { console.log("EXCEPTION CAUGHT:", e); } , but, of course, this will also lead to the immediate termination of the program.

So, at this moment I want to leave about 10 seconds so that the user can peek or take a screenshot before exiting.

I suppose I should just use sleep() lock through the npm module, but I found in testing that setting a timeout also seems to work. (that is, why bother with the module if something built-in works?) I assume that this is not significant, but I'm just wondering if it is indicated somewhere that node will really wait for all timeouts to complete before exiting, so that I can feel safe doing this.

+6
source share
6 answers

In general, node will wait for all timeouts to fire before logging out. The call to process.exit() will end before the timeout.

Details are part of libuv , but a vague comment is made in the documentation:

http://nodejs.org/api/all.html#all_ref

you can call ref () to explicitly request a timer to keep the program open

Combining all the facts together, setTimeout by default designed to keep the loop cycle open (therefore, if this is the only thing that expects, the program will wait). You can programmatically disable or reactivate the behavior.

+5
source

Late answer, but definite yes - Nodejs will wait for setTimeout to complete - see this documentation . By the way, there is also a way to not wait for setTimeout, and this is called unref for the object returned from setTimeout or setInterval .

To summarize: if you want Nodejs to wait until a timeout is called up, you have nothing to do. If you want Nodejs to not wait for a specific timeout, call unref .

+4
source

Easy Way Solution:

  • Make a batch (.bat) file that will run nodejs
  • make a shortcut out of it

Why is this the best. This way your client will run nodejs on the command line. And even if the nodejs program returns, nothing will happen to the command line.

Creating a bat file:

  • Make text file
  • put START cmd.exe /k "node abc.js"
  • Save
  • Rename it to abc.bat
  • make a shortcut or something else.
  • A CommandLine opens in the window that opens and launches the nodejs file.

Using setimeout for this is a bad idea.

+1
source

If node did not wait for all setTimeout or setInterval calls to complete, you cannot use them in simple scripts.

As soon as you tell node to listen for the event, just like with setTimeout or some asynchronous I / O call, the event loop will loop until it is prompted to exit.

Instead of wrapping everything in try/catch , you can bind the event listener to the process in the same way as the example in the docs:

 process.on('uncaughtException', function(err) { console.log('Caught exception: ' + err); }); setTimeout(function() { console.log('This will still run.'); }, 500); // Intentionally cause an exception, but don't catch it. nonexistentFunc(); console.log('This will not run.'); 

In the uncaughtException event uncaughtException you can add setTimeout to exit in 10 seconds:

 process.on('uncaughtException', function(err) { console.log('Caught exception: ' + err); setTimeout(function(){ process.exit(1); }, 10000); }); 

If you can recover this exception, you can look at the domains: http://nodejs.org/api/domain.html

edit:

There may actually be another problem: your client application is not doing enough (or any?) Of logging. You can use log4js-node to write in a temporary file or in a specific application.

+1
source

Odd is when you call process.exit() or there is an uncaught exception, as Jim Schubert pointed out. In addition, node will wait for the timeout to complete.

0
source

Node remembers timers, but only if it can track them. At least that's my experience.

If you use setTimeout in the arrow / anonymous function, I would recommend keeping track of your timers in an array, for example:

 => { timers.push(setTimeout(doThisLater, 2000)); } 

and make sure let timers = []; not set in a method that will fade, i.e. globally.

0
source

Source: https://habr.com/ru/post/951012/


All Articles