Read multiple files asynchronously
npm install read-multiple-files


Read multiple files Observable way
``javascript
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.');
}
});
`
``
npm install read-multiple-files
`javascript`
const readMultipleFiles = require('read-multiple-files');
paths: (file paths) Object
options: (fs.readFile options) or string (encoding) Observable
Return: (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:
`javascript`
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.
`javascript
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.`
}
});
* read-files-promise — Promise` interface version
ISC License © 2017 - 2018 Shinnosuke Watanabe