Closed
Description
It'd be great if readline
could emit 'data'
events for each line, so that for await
could be used:
const readline = require('readline');
const stream = require('stream');
const input = new stream.Readable();
input.push(`{"some": "json","another":"json"}\n`);
input.push(`{"some": "json2","another":"json2"}\n`);
input.push(null);
// What I wish I would do:
(async () => {
const rl = readline.createInterface({input});
const rows = [];
for await (const row of rl) rows.push(row);
console.log(rows)
})();
// workaround:
const betterReadLine = ({input}) => {
const output = new stream.PassThrough({objectMode: true});
const rl = readline.createInterface({input});
rl.on('line', line => {
output.write(JSON.parse(line));
});
rl.on('close', () => {
output.push(null);
});
return output;
};
(async () => {
const rl = betterReadLine({input});
const rows = [];
for await (const row of rl) rows.push(row);
console.log(rows)
})();