7

When I run the very code listed in the readline example

async function processLineByLine() {
  const fileStream = fs.createReadStream('input.txt');

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });
  // Note: we use the crlfDelay option to recognize all instances of CR LF
  // ('\r\n') in input.txt as a single line break.

  for await (const line of rl) {
    // Each line in input.txt will be successively available here as `line`.
    console.log(`Line from file: ${line}`);
  }
}

processLineByLine();

I get the following error:

(node:27485) UnhandledPromiseRejectionWarning: TypeError: rl is not async iterable                                                                    
    at processLineByLine (file:///home/ecarroll/code/toponym-esque/process.mjs:16:28)                                                                 
    at file:///home/ecarroll/code/toponym-esque/process.mjs:22:1
    at ModuleJob.run (internal/modules/esm/module_job.js:95:12)
(node:27485) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)                                                     
(node:27485) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468

1 Answers1

10

The changes to readline are a feature in Node v11.4.0.

readline: The readline module now supports async iterators. https://github.com/nodejs/node/pull/23916

You'll have to upgrade if you're running a prior version.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • 3
    Note: this feature appears on the v10 LTS documentation https://nodejs.org/docs/latest-v10.x/api/readline.html#readline_example_read_file_stream_line_by_line but yes, it fails. – noderman Jul 24 '19 at 02:24
  • 1
    I have it working with version 10.16.2 on Windows. I had a coworker running 10.15.3 on Mac and had him update to version 10.18.0 (the version I have on my Mac) and it also worked. – knueser Mar 30 '20 at 18:00