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.
source share