read-files-promise

Promise to read multiple files

read-files-promise

Promise to read multiple files

var readFiles = require('read-files-promise');
 
readFiles([
  'path/to/file0', // 'apple' 
  'path/to/file1', // 'orange' 
], {encoding: 'utf8'})
.then(function(buffers) {
  buffers; //=> ['apple', 'orange'] 
});

If you want to read a single file in the way of promise, use fs-readfile-promise.

Use npm.

npm install read-files-promise
var readFiles = require('read-files-promise');

filenames: Array of String (file paths)
options: Object or String (same as fs.readFile's second argument)
Return: Object (Promise)

It reads the files specified in its first argument.

When it finish reading all the files, it will be fulfilled with an array of the contents as its first argument. The order of the contents depends on the order of file paths.

When it fails to read at least one of the files, it will be rejected with an error as its first argument.

 
var readFiles = require('read-files-promise');
 
readFiles([
  'path/to/file0' // 'a' 
  'path/to/file1' // 'b' 
  'path/to/file2' // 'c' 
]).then(onFulfilled, onRejected);
 
function onFulfilled(buffers) {
  buffers; //=> [<Buffer 61>, <Buffer 62>, <Buffer 63>] 
};
 
function onRejected(err) {
  console.log('Cannot read the file.');
};

Copyright (c) 2014 Shinnosuke Watanabe

Licensed under the MIT License.