Skip to content

Instantly share code, notes, and snippets.

@dlmanning
Created November 12, 2013 03:29
Show Gist options
  • Save dlmanning/7425009 to your computer and use it in GitHub Desktop.
Save dlmanning/7425009 to your computer and use it in GitHub Desktop.
A function to asynchronously confirm the existence of every file in an array.
function ifFilesExist (files, cb) {
var numberOfFiles = files.length
, returnsChecked = 0
, existancesConfirmed = 0;
for (var i = 0; i < numberOfFiles; i++) {
fs.exists(files[i], next);
}
function next (e) {
returnsChecked++;
if (e) {
existancesConfirmed++;
}
if (returnsChecked >= numberOfFiles) {
if (existancesConfirmed >= numberOfFiles) {
cb(null);
} else {
cb(new Error("Not all files exist."));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment