Created
November 12, 2013 03:29
-
-
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.
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
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