-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed
Labels
experimentalIssues and PRs related to experimental features.Issues and PRs related to experimental features.feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.promisesIssues and PRs related to ECMAScript promises.Issues and PRs related to ECMAScript promises.readlineIssues and PRs related to the built-in readline module.Issues and PRs related to the built-in readline module.
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)
})();
Metadata
Metadata
Assignees
Labels
experimentalIssues and PRs related to experimental features.Issues and PRs related to experimental features.feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.promisesIssues and PRs related to ECMAScript promises.Issues and PRs related to ECMAScript promises.readlineIssues and PRs related to the built-in readline module.Issues and PRs related to the built-in readline module.