Readline using promises
npm install readline-promiseA drop in replacement for readline with additional promise based methods like map, reduce, and forEach. Uses a custom AsyncIterator implementation.
Note: If you were using var readline = require('readline'), change this to var readline = require('readline-promise').default.
js
import readline from 'readline-promise';
import fs from 'fs';const rlp = readline.createInterface({
terminal: false,
input: fs.createReadStream('data.txt')
});
rlp.forEach((line, index) => {
console.log({ line, index });
});
`Example
`js
import readline from 'readline-promise';const rlp = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true
});
let bar = null;
rlp.questionAsync('Foo?').then(answer => {
bar = answer;
});
`Extended API
readline-promise adds the following methods$3
Creates a new line reader Interface (
rlp)*
options <Object> see interface options for details$3
Iterates through each line calling an
iteratee function with the value. See Array.forEach documentation for details*
iteratee <Function>
line <> - line value
* index <Number> - the line index starting from 0
lines <Array<>> - all current line values as an array$3
Alias for
rlp.forEach$3
Performs a map operation using the iteratee function. See Array.map documentation for details
*
iteratee <Function>
line <> - line value
* index <Number> - the line index starting from 0
lines <Array<>> - all current line values as an array$3
Performs a reduce operation using the iteratee and optional accumulator/initial value. See Array.reduce documentation for details
*
iteratee <Function>
accumulator <> - accumulator value
line <> - line value
* index <Number>- the line index starting from 0
lines <Array<>> - all current line values as an array
[accumulator] <> - Optional initial value$3
Performs a question that returns a Promise that resolves to the answer value
*
query` <String> - prompt text