diff options
author | Vse Mozhet Byt <vsemozhetbyt@gmail.com> | 2019-03-06 17:58:53 +0300 |
---|---|---|
committer | Vse Mozhet Byt <vsemozhetbyt@gmail.com> | 2019-03-08 19:09:21 +0300 |
commit | 15e741a1320638a1fcf108d65b3338142769eecb (patch) | |
tree | f19bc96a7d418cb987f1bf2ad541a29d3bd15620 /doc/api/readline.md | |
parent | 9f1282d536dc8f0a11714980b4b78ee0b979e1de (diff) |
doc: add caveat and tradeoff example to readline
PR-URL: https://github.com/nodejs/node/pull/26472
Refs: https://github.com/nodejs/node/pull/23916
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'doc/api/readline.md')
-rw-r--r-- | doc/api/readline.md | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/doc/api/readline.md b/doc/api/readline.md index 41e1da0a6aa..56a5254adcc 100644 --- a/doc/api/readline.md +++ b/doc/api/readline.md @@ -597,6 +597,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 [`'line'`]: #readline_event_line |