Created
January 22, 2014 01:12
-
-
Save tomshen/8551814 to your computer and use it in GitHub Desktop.
Demonstrating three different ways to delete files whose names are stored 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
var companies = [/* ... */]; | |
/* WITH SYNC */ | |
var fs = require("fs"); | |
companies.forEach(fs.unlinkSync(company)); // this is blocking | |
console.log("All files deleted"); | |
/* WITH CALLBACKS */ | |
var fs = require("fs"); | |
var unlinkCount = companies.length; | |
companies.forEach(function (company) { | |
fs.unlink(company, function () { | |
unlinkCount--; | |
if (unlinkCount === 0) | |
console.log("All files deleted"); | |
}); | |
}); | |
/* WITH PROMISES */ | |
var fs = require("q-io/fs"); | |
var Promise = require("q"); | |
var companyDeletionPromises = companies.map(fs.remove(company)); | |
Promise.all(companyDeletionPromises).done(function () { | |
console.log("All files deleted"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment