Skip to content

bhoriuchi/readline-promise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 methods

readline.createInterface(options) → rlp

Creates a new line reader Interface (rlp)

rlp.forEach(iteratee) → Promise<undefined>

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

rlp.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 details

  • iteratee <Function>
    • line <*> - line value
    • index <Number> - the line index starting from 0
    • lines <Array<*>> - all current line values as an array

rlp.reduce(iteratee [, accumulator]) → Promise<*>

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

rlp.questionAsync(query) → Promise<*>

Performs a question that returns a Promise that resolves to the answer value

  • query <String> - prompt text