Last active
March 15, 2017 01:00
-
-
Save sebadoom/f4d581815c75bbe06ed198f84fb0551c to your computer and use it in GitHub Desktop.
Using console.log can change program state.
This file contains 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
/* 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