Last active
September 22, 2015 13:52
-
-
Save jugglinmike/3e8288d96bdb826e68df to your computer and use it in GitHub Desktop.
Experiment for Tim!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var spawn = require('child_process').spawn; | |
function parent() { | |
var child = spawn('node', [__filename, 'something else']); | |
child.on('exit', function() { | |
console.log('The child has exited.'); | |
}); | |
child.stdout.on('data', function(chunk) { | |
console.log('> ' + chunk); | |
if (/\(iteration #8\)/.test(chunk)) { | |
child.kill(); | |
} | |
}); | |
console.log('I am the parent.'); | |
console.log('Child data:'); | |
} | |
function child() { | |
var idx = 0; | |
setInterval(function() { | |
console.log('I am the child (iteration #' + (idx++) + ')'); | |
}, 100); | |
} | |
process.argv.length === 2 ? parent() : child(); | |
/** | |
* On my machine: | |
* | |
* $ node index.js | |
* I am the parent. | |
* Child data: | |
* > I am the child (iteration #0) | |
* | |
* > I am the child (iteration #1) | |
* | |
* > I am the child (iteration #2) | |
* | |
* > I am the child (iteration #3) | |
* | |
* > I am the child (iteration #4) | |
* | |
* > I am the child (iteration #5) | |
* | |
* > I am the child (iteration #6) | |
* | |
* > I am the child (iteration #7) | |
* | |
* > I am the child (iteration #8) | |
* | |
* The child has exited. | |
* | |
* $ node --version | |
* v4.1.0 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment