Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to define the Node.js status code without exiting immediately?

How to define the Node.js status code without exiting immediately?

Problem

In Node, the process.exit(code) method allows one to exit with the code status code.

However, what I am looking for is a way to define, under some condition that are not temporally defined, the status code with which the process will exit, without exiting immediately.

How can I do such a thing?

Problem courtesy of: MattiSG

Solution

Use the process's exit event.

Even though it has the same name as the method, an exit handler will not be called recursively if you call process.exit from within:

var code = 0;
// ...
code = 1
// ...
process.on('exit', function() {
    process.exit(code);
});
Solution courtesy of: MattiSG

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How to define the Node.js status code without exiting immediately?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×