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

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?

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);
});