Last active
August 5, 2021 12:11
-
-
Save Sequoia/41a9c8be2ff2441357f41fddd3f68124 to your computer and use it in GitHub Desktop.
Get File Contents of Multiple Files as a Promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Promise from 'bluebird'; | |
import {readFiles} from 'node-dir'; | |
import {l} from './util'; | |
/** | |
* @param {String} root path | |
* @param {Object} options see: https://www.npmjs.com/package/node-dir | |
* @return {Promise<String[]>} | |
*/ | |
export default function getFiles(root, options){ | |
let contents = []; | |
return new Promise((resolve, reject) => { | |
readFiles(root, options, | |
function(err, content, next) { | |
if (err) throw err; | |
contents.push(content); | |
next(); | |
}, | |
function(err, filenames){ | |
if (err) return reject(err); | |
l('finished reading files'); | |
resolve(contents); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why ES6