Skip to content

Instantly share code, notes, and snippets.

@oliverjumpertz
Last active May 9, 2020 17:38
Show Gist options
  • Save oliverjumpertz/62b6c18a962da08d142491569535044d to your computer and use it in GitHub Desktop.
Save oliverjumpertz/62b6c18a962da08d142491569535044d to your computer and use it in GitHub Desktop.
readStdIn is an awaitable naive function which reads all input from stdin as a single, new-line preserved string,
import readline from 'readline';
async function readStdIn() {
return new Promise((resolve, reject) => {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
let input = '';
rl.on('line', (line) => {
input += line;
input += '\n';
});
rl.on('close', () => {
resolve(input);
});
// in case no input is provided, we don't want to block forever
setTimeout(() => {
if (input === '') {
rl.close();
}
}, 50);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment