A 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
.
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 });
});
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;
});
readline-promise
adds the following methods
Creates a new line reader Interface (rlp
)
options
<Object> see interface options for details
Iterates through each line calling an iteratee
function with the value. See Array.forEach documentation for details
iteratee
<Function>line
<*> - line valueindex
<Number> - the line index starting from 0lines
<Array<*>> - all current line values as an array
Alias for rlp.forEach
Performs a map operation using the iteratee function. See Array.map documentation for details
iteratee
<Function>line
<*> - line valueindex
<Number> - the line index starting from 0lines
<Array<*>> - all current line values as an array
Performs a reduce operation using the iteratee and optional accumulator/initial value. See Array.reduce documentation for details
iteratee
<Function>accumulator
<*> - accumulator valueline
<*> - line valueindex
<Number>- the line index starting from 0lines
<Array<*>> - all current line values as an array
[accumulator]
<*> - Optional initial value
Performs a question that returns a Promise that resolves to the answer value
query
<String> - prompt text