Skip to content

Instantly share code, notes, and snippets.

@lxe
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save lxe/0d5331b7afe6aa1bbcf7 to your computer and use it in GitHub Desktop.

Select an option

Save lxe/0d5331b7afe6aa1bbcf7 to your computer and use it in GitHub Desktop.
function listenForInput (callback) {
process.stdout.write('Listening for input:');
function onReadable () {
var chunk = process.stdin.read();
if (!chunk) return;
console.log('Received: ', chunk.toString());
// Remove the readable listener
process.stdin.removeListener('readable', onReadable);
// // Random trial and error with no success:
// process.stdin.end();
// process.stdin.push(null);
// process.stdin.unshift(null);
// process.stdin.read(0);
// process.stdin._readableState.calledRead = false
// process.stdin._readableState.needReadable = false
// process.stdin._readableState.readableListening = false
// process.stdin._readableState.reading = false
console.log('Let\'s move on to the next one!');
callback();
}
process.stdin.on('readable', onReadable);
}
function firstInputListener () {
listenForInput(secondInputListener)
}
function secondInputListener () {
listenForInput(finish);
}
function finish () {
// We're done! I shouldn't have to call process.exit() OR wait for inputs!
console.log('For some reason you have to enter another line! ( or just press enter) :(');
console.log('Look! No "readable" events:');
console.log(process.stdin._events);
console.log('Press enter now... for whatever the reason... or process.exit()...')
}
firstInputListener();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment