dpjayasekara
4/8/2017 - 7:16 PM

Advanced NodeJS Snippets

Advanced NodeJS Snippets

console.log('start of the script');

process.on('uncaughtException', (e) => {
    console.log('uncaught exception occurred during execution! quitting...');
    console.error(e.message);
    process.exit(1);
});

process.on('exit', (code) => {
    console.log(`process exit event handler called with error code ${code}!`);
});

process.on('beforeExit', (code) => {
    // this won't run when process.exit method is explicitly called
    console.log(`process beforeExit event handler called with error code ${code}!`);
});

throw new Error('oops!');
function test(value, cb) {
    if (value === 0){
        return process.nextTick(cb);
    }

    setTimeout(cb, 0);
}

test(0,() => console.log('callback called'));
console.log('should run before calling callback');
process.nextTick(() => console.log('this is the callback of last process next tick'));