Skip to content

Readline: emit data events along with current line events #23032

Closed
@caub

Description

@caub

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

No one assigned

    Labels

    experimentalIssues and PRs related to experimental features.feature requestIssues that request new features to be added to Node.js.promisesIssues and PRs related to ECMAScript promises.readlineIssues and PRs related to the built-in readline module.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions