readline-promise

2019-12-03

readline-promise是什么

什么是readline-promise,Readline using promises

readline-promise使用教程帮助文档

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

上一篇:webpack 图片路劲和打包
下一篇:slib
相关文章

首次访问,人机识别验证

扫描下方二维码回复 1024 获取验证码,验证完毕后 永久 无须验证

操作步骤:[打开微信]->[扫描上侧二维码]->[关注 FedJavaScript 的微信] 输入 1024 获取验证码

验证码有误,请重新输入