Created
May 14, 2012 08:32
-
-
Save mrutid/2692722 to your computer and use it in GitHub Desktop.
Dynamic module loading (async)
This file contains hidden or 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 async = require('async'); | |
var fs = require('fs'); | |
var initArr = []; | |
var options = 'EmitterFoo'; | |
var files = fs.readdirSync("./listeners/"); //better sync | |
console.log('Files Readed:' + files); | |
files.forEach(function(element) { | |
var module = require("./listeners/" + element); | |
initArr.push(function(cb) { | |
module.init(options, cb) | |
}); | |
}); | |
//init and continue (if init is async) | |
async.parallel(initArr, function(err, result) { | |
if (err) { | |
console.log('error in async call:' + err) | |
} else { | |
console.log('all modules loaded'); | |
} | |
//call main or continue | |
}); | |
//It would be nice if it could be Synchronous (no nesting of Main code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment