Skip to content

Instantly share code, notes, and snippets.

@cmdcolin
Last active March 29, 2019 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmdcolin/e88307f6255c85ac534b903a09b73825 to your computer and use it in GitHub Desktop.
Save cmdcolin/e88307f6255c85ac534b903a09b73825 to your computer and use it in GitHub Desktop.
// from https://stackoverflow.com/questions/43638105/how-to-get-synchronous-readline-or-simulate-it-using-async-in-nodejs
const readline=require('readline')
const rl = readline.createInterface({ input: process.stdin , output: process.stdout });
const getLine = (function () {
const getLineGen = (async function* () {
for await (const line of rl) {
yield line;
}
})();
return async () => ((await getLineGen.next()).value);
})();
const main = async () => {
const a = await getLine()
const b = await getLine()
console.log('first two lines', a, b);
const lines = []
while ((line = await getLine())){
lines.push(line)
}
console.log('rest', lines)
process.exit(0);
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment