Skip to content

Commit

Permalink
doc: add caveat and tradeoff example to readline
Browse files Browse the repository at this point in the history
PR-URL: #26472
Refs: #23916
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
vsemozhetbyt authored and BethGriggs committed Apr 16, 2019
1 parent 479ef60 commit 4065785
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,34 @@ rl.on('line', (line) => {
});
```

Currently, `for`-`await`-`of` loop can be a bit slower. If `async` / `await`
flow and speed are both essential, a mixed approach can be applied:

```js
const { once } = require('events');
const { createReadStream } = require('fs');
const { createInterface } = require('readline');

(async function processLineByLine() {
try {
const rl = createInterface({
input: createReadStream('big-file.txt'),
crlfDelay: Infinity
});

rl.on('line', (line) => {
// Process the line.
});

await once(rl, 'close');

console.log('File processed.');
} catch (err) {
console.error(err);
}
})();
```

[`'SIGCONT'`]: readline.html#readline_event_sigcont
[`'SIGTSTP'`]: readline.html#readline_event_sigtstp
[`process.stdin`]: process.html#process_process_stdin
Expand Down

0 comments on commit 4065785

Please sign in to comment.