@@ -309,6 +309,38 @@ rl.write(null, { ctrl: true, name: 'u' });
309309The ` rl.write() ` method will write the data to the ` readline ` ` Interface ` 's
310310` input ` * as if it were provided by the user* .
311311
312+ ### rl\[ Symbol.asyncIterator\] ()
313+ <!-- YAML
314+ added: REPLACEME
315+ -->
316+
317+ > Stability: 1 - Experimental
318+
319+ * Returns: {AsyncIterator}
320+
321+ Create an ` AsyncIterator ` object that iterates through each line in the input
322+ stream as a string. This method allows asynchronous iteration of
323+ ` readline.Interface ` objects through ` for ` -` await ` -` of ` loops.
324+
325+ * Note:* Errors in the input stream are not forwarded.
326+
327+ If the loop is terminated with ` break ` , ` throw ` , or ` return ` ,
328+ [ ` rl.close() ` ] [ ] will be called. In other words, iterating over a
329+ ` readline.Interface ` will always consume the input stream fully.
330+
331+ ``` js
332+ async function processLineByLine () {
333+ const rl = readline .createInterface ({
334+ // ...
335+ });
336+
337+ for await (const line of rl ) {
338+ // Each line in the readline input will be successively available here as
339+ // `line`.
340+ }
341+ }
342+ ```
343+
312344## readline.clearLine(stream, dir)
313345<!-- YAML
314346added: v0.7.7
@@ -517,12 +549,38 @@ rl.on('line', (line) => {
517549
518550## Example: Read File Stream Line-by-Line
519551
520- A common use case for ` readline ` is to consume input from a filesystem
521- [ Readable] [ ] stream one line at a time:
552+ A common use case for ` readline ` is to consume an input file one line at a
553+ time. The easiest way to do so is leveraging the [ ` fs.ReadStream ` ] [ ] API as
554+ well as a ` for ` -` await ` -` of ` loop:
522555
523556``` js
557+ const fs = require (' fs' );
524558const readline = require (' readline' );
559+
560+ async function processLineByLine () {
561+ const fileStream = fs .createReadStream (' input.txt' );
562+
563+ const rl = readline .createInterface ({
564+ input: fileStream,
565+ crlfDelay: Infinity
566+ });
567+ // Note: we use the crlfDelay option to recognize all instances of CR LF
568+ // ('\r\n') in input.txt as a single line break.
569+
570+ for await (const line of rl ) {
571+ // Each line in input.txt will be successively available here as `line`.
572+ console .log (` Line from file: ${ line} ` );
573+ }
574+ }
575+
576+ processLineByLine ();
577+ ```
578+
579+ Alternatively, one could use the [ ` 'line' ` ] [ ] event:
580+
581+ ``` js
525582const fs = require (' fs' );
583+ const readline = require (' readline' );
526584
527585const rl = readline .createInterface ({
528586 input: fs .createReadStream (' sample.txt' ),
@@ -536,8 +594,11 @@ rl.on('line', (line) => {
536594
537595[ `'SIGCONT'` ] : readline.html#readline_event_sigcont
538596[ `'SIGTSTP'` ] : readline.html#readline_event_sigtstp
597+ [ `'line'` ] : #readline_event_line
598+ [ `fs.ReadStream` ] : fs.html#fs_class_fs_readstream
539599[ `process.stdin` ] : process.html#process_process_stdin
540600[ `process.stdout` ] : process.html#process_process_stdout
601+ [ `rl.close()` ] : #readline_rl_close
541602[ Readable ] : stream.html#stream_readable_streams
542603[ TTY ] : tty.html
543604[ Writable ] : stream.html#stream_writable_streams
0 commit comments