I am wondering if there is an easy way to get a “synchronous” readline, or at least get synchronous I / O in node.js
I use something like this, but it is rather inconvenient
var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: false }); var i = 0; var s1 = ''; var s2 = ''; rl.on('line', function(line){ if(i==0) { s1 = line; } else if(i==1) { s2 = line; } i++; }) rl.on('close', function() {
Instead, I would prefer it to be as simple as something like
var s1 = getline(); // or "await getline()?" var s2 = getline(); // or "await getline()?"
Useful conditions:
(a) It is not recommended to use external modules or a file descriptor / dev / stdio, I send the code to the code submission website and they do not work there
(b) May use async / wait or generators
(c) Must be based on a string
(d) No need to read the entire stdin file into memory before processing
Colin d
source share