Steven Lu
Steven Lu

Reputation: 43487

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

Consider:

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

This will actually wait for the timeout to fire before the program exits.

I am basically wondering if this means that node is intended to wait for all timeouts to complete before quitting.

Here is my situation. My client has a node.js server he's gonna run from Windows with a Shortcut icon. If the node app encounters an exceptional condition, it will typically instantly exit, not leaving enough time to see in the console what the error was, and this is bad.

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

So at this point I want to leave about 10 seconds for the user to take a peek or screenshot of the exception before it quits.

I figure I should just use blocking sleep() through the npm module, but I discovered in testing that setting a timeout also seems to work. (i.e. why bother with a module if something builtin works?) I guess the significance of this isn't big, but I'm just curious about whether it is specified somewhere that node will actually wait for all timeouts to complete before quitting, so that I can feel safe doing this.

Upvotes: 17

Views: 14374

Answers (6)

webjay
webjay

Reputation: 5508

Node does remember timers, but only if it can keep track of them. At least that is my experience.

If you use setTimeout in an arrow / anonymous function I would recommend to keep track of your timers in an array, like:

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

and make sure let timers = []; isn't set in a method that will vanish, so i.e. globally.

Upvotes: 0

Erwin Wessels
Erwin Wessels

Reputation: 3103

Late answer, but a definite yes - Nodejs will wait around for setTimeout to finish - see this documentation. Coincidentally, there is also a way to not wait around for setTimeout, and that is by calling unref on the object returned from setTimeout or setInterval.

To summarize: if you want Nodejs to wait until the timeout has been called, there's nothing you need to do. If you want Nodejs to not wait for a particular timeout, call unref on it.

Upvotes: 7

quinnirill
quinnirill

Reputation: 834

The odd ones out are when you call process.exit() or there's an uncaught exception, as pointed out by Jim Schubert. Other than that, node will wait for the timeout to complete.

Upvotes: 0

Muhammad Umer
Muhammad Umer

Reputation: 2308

Easy way Solution:

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

Why this is best. This way you client would run nodejs in command line. And even if nodejs program returns nothing would happen to command line.

Making bat file:

  1. Make a text file
  2. put START cmd.exe /k "node abc.js"
  3. Save it
  4. Rename It to abc.bat
  5. make a shortcut or whatever.
  6. Opening it will Open CommandLine and run nodejs file.

using settimeout for this is a bad idea.

Upvotes: 1

Jim Schubert
Jim Schubert

Reputation: 20357

If node didn't wait for all setTimeout or setInterval calls to complete, you wouldn't be able to use them in simple scripts.

Once you tell node to listen for an event, as with the setTimeout or some async I/O call, the event loop will loop until it is told to exit.

Rather than wrap everything in a try/catch you can bind an event listener to process just 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, you can then add a setTimeout to exit after 10 seconds:

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

If this exception is something you can recover from, you may want to look at domains: http://nodejs.org/api/domain.html

edit:

There may actually be another issue at hand: your client application doesn't do enough (or any?) logging. You can use log4js-node to write to a temp file or some application-specific location.

Upvotes: 1

SheetJS
SheetJS

Reputation: 22925

In general, node will wait for all timeouts to fire before quitting normally. Calling process.exit() will exit before the timeouts.

The details are part of libuv, but the documentation makes a vague comment about it:

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

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

Putting all of the facts together, setTimeout by default is designed to hold the event loop open (so if that's the only thing pending, the program will wait). You can programmatically disable or re-enable the behavior.

Upvotes: 14

Related Questions