Skip to content

Instantly share code, notes, and snippets.

@sebadoom
Last active March 15, 2017 01:00
Show Gist options
  • Save sebadoom/f4d581815c75bbe06ed198f84fb0551c to your computer and use it in GitHub Desktop.
Save sebadoom/f4d581815c75bbe06ed198f84fb0551c to your computer and use it in GitHub Desktop.
Using console.log can change program state.
/* To run:
* mkfifo fifo
* node console-state.js | tee fifo
* Wait 2 seconds, then look for state.json in the same directory.
*/
const fs = require('fs');
const state = [];
const stream = fs.createReadStream('fifo');
//const stream = process.stdin;
stream.on('data', d => {
state.push(d.toString());
});
stream.on('error', e => {
console.log(e);
});
setTimeout(() => {
// If console.log is pure, state.json should read '[]'.
console.log('a');
console.log('b');
console.log('c');
console.log('d');
}, 1000);
setTimeout(() => {
fs.writeFileSync('state.json', JSON.stringify(state));
}, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment