Skip to content

Instantly share code, notes, and snippets.

@famasya
Created January 10, 2016 04:05
Show Gist options
  • Save famasya/18d592222c44d68bdfc4 to your computer and use it in GitHub Desktop.
Save famasya/18d592222c44d68bdfc4 to your computer and use it in GitHub Desktop.
[How to] using async.js
var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
var configs = {};
async.forEachOf(obj, function (value, key, callback) {
fs.readFile(__dirname + value, "utf8", function (err, data) {
if (err) return callback(err);
try {
configs[key] = JSON.parse(data);
} catch (e) {
return callback(e);
}
callback();
})
}, function (err) {
if (err) console.error(err.message);
// configs is now a map of JSON data
doSomethingWith(configs);
})
//snippet originally from https://github.com/caolan/async
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment