Skip to content

Instantly share code, notes, and snippets.

@mrutid
Created May 14, 2012 08:32
Show Gist options
  • Save mrutid/2692722 to your computer and use it in GitHub Desktop.
Save mrutid/2692722 to your computer and use it in GitHub Desktop.
Dynamic module loading (async)
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