Skip to content

Instantly share code, notes, and snippets.

@heikki
Last active August 29, 2015 14:12
Show Gist options
  • Save heikki/8b11d5bb918ecb9de6bf to your computer and use it in GitHub Desktop.
Save heikki/8b11d5bb918ecb9de6bf to your computer and use it in GitHub Desktop.
{
"dependencies": {
"exit": "^0.1.2"
}
}
'use strict';
// https://github.com/cowboy/node-exit
var exit = require('exit');
console.log('before');
process.on('exit', function() {
console.log('on exit');
});
console.log('after');
exit();
'use strict';
console.log('before');
process.on('exit', function() {
console.log('on exit');
});
console.log('after');
process.exit();
'use strict';
// From https://github.com/airportyh/testem/blob/master/lib/clean_exit.js
function exit(code) {
var draining = 0;
var onDrain = function() {
if (!(draining--)) {
process.exit(code);
}
};
if (process.stdout.bufferSize) {
draining++;
process.stdout.once('drain', onDrain);
}
if (process.stderr.bufferSize) {
draining++;
process.stderr.once('drain', onDrain);
}
if (!draining) {
process.exit(code);
}
}
console.log('before');
process.on('exit', function() {
console.log('on exit');
});
console.log('after');
exit();
'use strict';
var child = require('child_process');
child.exec('node test-process-exit.js', function(stderr, stdout) {
console.log('\n--- process.exit, stdout is truncated on windows ---');
console.log('stderr:', stderr);
console.log('stdout:', stdout);
});
child.exec('node test-node-exit.js', function(stderr, stdout) {
console.log('\n--- node-exit, \'on exit\' is missing ---');
console.log('stderr:', stderr);
console.log('stdout:', stdout);
});
child.exec('node test-testem-exit.js', function(stderr, stdout) {
console.log('\n--- testem exit, all ok? ---');
console.log('stderr:', stderr);
console.log('stdout:', stdout);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment