Does anyone have any idea why nodeJS and Async are doing this?
nodejs·@kaptainkrayola·
0.000 HBDDoes anyone have any idea why nodeJS and Async are doing this?
<html> <p>I have been working on a node module today and while trying out some new syntax/code organization I ran into some funky behavior that I have never seen before. </p> <p>I normally create modules that look like this :</p> <pre><code>var async = require("async");<br> module.exports = function(config){<br> var self = this;<br> self.someFunct = function(arg, callback){<br> // do some stuff<br> callback(err, results);<br> }<br> }</code></pre> <p>Then, I'll just require the module and do a</p> <pre><code>var myModule = require("myModule");<br> myModule = new myModule(config);</code></pre> <p> I have been looking to reduce the lines of code required to setup my modules so I started adding a "return this" to the bottom of the module so I could require it like this:</p> <pre><code>var myModule = require("myModule")(config);</code></pre> <p>This works great most of the time. Today I ran into something funky and I have no idea why. </p> <p>I started to set up a service that required both SQL Server and Redis modules. No problem, I have those so I'll use them with the new syntax.</p> <pre><code>var config = require("../config");<br> var database = require("data/sqlServer")(config.database);<br> var redis = require("data/redis")(config.redis);<br> </code></pre> <pre><code>//app startup<br> async.series([<br> loadConnections,<br> loadModules,<br> startServer<br> ], function(err){<br> console.log("Everything loaded.")<br> })</code></pre> <p> I use async.parallel to load the connections to Redis and SQL Server.</p> <pre><code>function loadConnections(allDone){ <br> async.series([ <br> redis.init, <br> database.init <br> ], allDone)}</code></pre> <p>What happened with that one is that somehow, "database" gets overwritten by "redis" so instead of calling .init() on the database module it calls .init() on redis twice and a console.log(database) shows it as redis. wut?</p> <p>I also ran into the situation where somehow the redis module would get the config for the database module and would crash trying to start because the config wasn't what it was expecting. </p> <p>The really confusing part is that if I set the code up like this it works fine.</p> <pre><code>var database = require("data/sqlServer");<br> var redis = require("data/redis");<br> function loadConnections(allDone){ <br> async.series([ <br> function(done){ <br> redis = redis(config.redis) <br> redis.init(done) <br> }, function(done){ <br> database = database(config.mssql); <br> database.init(done) } <br> ], allDone)<br> }</code></pre> <p>If I change async.series to async.parallel there I get the "config" on the Redis module that of the Database module instead of the Redis module. However, if I change it to use "new" it works fine :</p> <pre><code>var database = require("data/sqlServer");<br> var redis = require("data/redis"); <br> function loadConnections(allDone){ <br> async.series([ <br> function(done){ <br> redis = new redis(config.redis) <br> redis.init(done) <br> }, <br> function(done){ <br> database = new database(config.mssql);<br> database.init(done) <br> } <br> ], allDone) <br> }</code></pre> <p>So overall I have no idea why node is behaving this way. It could be something with the Async module but that is pretty tried and true so I'm not sure. I suppose I should try it without Async and just with some nested callbacks and see if I run into the same thing </p> </html>