JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 9918
  • Score
    100M100P100Q171427F
  • License ISC

Read multiple files asynchronously

Package Exports

  • read-multiple-files

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (read-multiple-files) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

read-multiple-files

npm version Build Status 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 (zenparsing'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.
  }
});

License

ISC License © 2017 Shinnosuke Watanabe