Given that your app is running on a treadmill (like the example above), all you need to do to handle graceful shutdowns (such that the last running loop will always finish up), is the following:

var Nator =require('nator');

vardoSomething=function(){

// do something here

doSomethingAgain();

}

vardoSomethingAgain=function(){

doSomething();// again!

}

var nator =newNator.handler(doSomething);

nator.start();

Optionally, you may pass a custom shutdown function to the .start() that will execute upon receiving SIGINT, SIGHUP or SIGTERM signals. You will need to issue process.exit() on your own inside the shutdown function when doing so.