read-multiple-files
Read multiple files asynchronously
Last updated 6 years ago by shinnn .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install read-multiple-files 
SYNC missed versions from official npm registry.

read-multiple-files

npm version Build Status Coverage Status

Read multiple files Observable way

const readMultipleFiles = require('read-multiple-files');

readMultipleFiles(new Set([
  'one.txt',    // 'a'
  'another.txt' // 'b'
])).subscribe({
  next(result) {
    if (result.path === 'one.txt') {
      result.contents; // Buffer.from('a')
    } else if (result.path === 'another.txt') {
      result.contents; // Buffer.from('b')
    }
  },
  complete() {
    console.log('Successfully read all files.');
  }
});

Installation

Use npm.

npm install read-multiple-files

API

const readMultipleFiles = require('read-multiple-files');

readMultipleFiles(paths [, options])

paths: <Array|Set<string|Buffer|URL|integer>> (file paths)
options: Object (fs.readFile options) or string (encoding)
Return: Observable (Kevin Smith's implementation)

When the Observable is subscribed, it starts to read files in parallel, successively send each result to its Observer as an Object: {path: <string|Buffer|URL|integer>, contents: <string:Buffer>}

readMultipleFiles([
  'foo.txt', // 'Hello'
  'bar.txt'  // 'World'
], 'utf8').subscribe({
  next({path, contents}) {
    if (path === 'one.txt') {
      contents; // 'Hello'
    } else if (path === 'another.txt') {
      contents; // 'World'
    }
  }
});

The Observer receives an error when it fails to read at least one of the files.

const readMultipleFiles = require('read-multiple-files');

readMultipleFiles([
  'foo.txt', // exists
  'bar.txt'  // doesn't exist
]).subscribe({
  error(err) {
    err.code; //=> ENOENT
  },
  complete() {
    // `complete` callback will never be called.
  }
});

Related project

License

ISC License © 2017 - 2018 Shinnosuke Watanabe

Current Tags

  • 3.0.0                                ...           latest (2 years ago)
  • 3.0.0-0                                ...           next (2 years ago)

12 Versions

  • 3.0.0                                ...           2 years ago
  • 3.0.0-0                                ...           2 years ago
  • 2.0.0                                ...           3 years ago
  • 2.0.0-4                                ...           3 years ago
  • 2.0.0-3                                ...           3 years ago
  • 2.0.0-2                                ...           3 years ago
  • 2.0.0-1                                ...           3 years ago
  • 2.0.0-0                                ...           3 years ago
  • 1.1.1                                ...           5 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 13
Last Month 50
Dependencies (2)
Dev Dependencies (7)

Copyright 2014 - 2017 © taobao.org |