readline-promise
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
.Example
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
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 methodsreadline.createInterface(options) → rlp
Creates a new line reader Interface (rlp
)options
<Object> see interface options for details
rlp.forEach(iteratee) → Promise<undefined>
Iterates through each line calling an iteratee
function with the value. See Array.forEach documentation for detailsiteratee
<Function>
line
<> - line value
index
<Number> - the line index starting from 0
lines
<Array<>> - all current line values as an arrayrlp.each(iteratee) → Promise<undefined>
Alias for rlp.forEach
rlp.map(iteratee) → Promise<Array<*>>
Performs a map operation using the iteratee function. See Array.map documentation for detailsiteratee
<Function>
line
<> - line value
index
<Number> - the line index starting from 0
lines
<Array<>> - all current line values as an arrayrlp.reduce(iteratee [, accumulator]) → Promise<*>
Performs a reduce operation using the iteratee and optional accumulator/initial value. See Array.reduce documentation for detailsiteratee
<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
rlp.questionAsync(query) → Promise<*>
Performs a question that returns a Promise that resolves to the answer valuequery
<String> - prompt text