Created
January 10, 2016 04:05
-
-
Save famasya/18d592222c44d68bdfc4 to your computer and use it in GitHub Desktop.
[How to] using async.js
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 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