create account

Does anyone have any idea why nodeJS and Async are doing this? by kaptainkrayola

View this thread on: hive.blogpeakd.comecency.com
· @kaptainkrayola ·
$154.63
Does 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.&nbsp;</p>
<p>I normally create modules that look like this :</p>
<pre><code>var async = require("async");<br>
module.exports = function(config){<br>
 &nbsp;&nbsp;&nbsp;var self = this;<br>
 &nbsp;&nbsp;&nbsp;self.someFunct = function(arg, callback){<br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// do some stuff<br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callback(err, results);<br>
 &nbsp;&nbsp;&nbsp;}<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>&nbsp;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. &nbsp;Today I ran into something funky and I have no idea why.&nbsp;</p>
<p>I started to set up a service that required both SQL Server and Redis modules. &nbsp;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>
 &nbsp;&nbsp;&nbsp;loadConnections,<br>
 &nbsp;&nbsp;&nbsp;loadModules,<br>
 &nbsp;&nbsp;&nbsp;startServer<br>
], function(err){<br>
 &nbsp;&nbsp;&nbsp;console.log("Everything loaded.")<br>
})</code></pre>
<p>&nbsp;I use async.parallel to load the connections to Redis and SQL Server.</p>
<pre><code>function loadConnections(allDone){ <br>
 &nbsp;&nbsp;&nbsp;async.series([ <br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redis.init, <br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database.init	<br>
 &nbsp;&nbsp;&nbsp;], 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. &nbsp;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.&nbsp;</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>
 &nbsp;&nbsp;&nbsp;async.series([ <br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function(done){			<br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redis = redis(config.redis) <br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redis.init(done)		<br>
 &nbsp;&nbsp;&nbsp;}, function(done){			<br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database = database(config.mssql); <br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database.init(done)		}	<br>
 &nbsp;&nbsp;&nbsp;], 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. &nbsp;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){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
 &nbsp;&nbsp;&nbsp;async.series([ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function(done){			 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redis = new redis(config.redis) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redis.init(done)		 &nbsp;&nbsp;&nbsp;&nbsp;<br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, <br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function(done){			 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database = new database(config.mssql);<br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database.init(done)		<br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}	 &nbsp;&nbsp;<br>
 &nbsp;&nbsp;], allDone) <br>
}</code></pre>
<p>So overall I have no idea why node is behaving this way. &nbsp;It could be something with the Async module but that is pretty tried and true so I'm not sure. &nbsp;I suppose I should try it without Async and just with some nested callbacks and see if I run into the same thing&nbsp;</p>
</html>
👍  , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorkaptainkrayola
permlinkdoes-anyone-have-any-idea-why-nodejs-and-async-are-doing-this
categorynodejs
json_metadata{"tags":["nodejs"]}
created2016-07-14 02:08:45
last_update2016-07-14 02:08:45
depth0
children1
last_payout2016-08-14 03:25:27
cashout_time1969-12-31 23:59:59
total_payout_value115.983 HBD
curator_payout_value38.648 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,978
author_reputation2,518,560,260,114
root_title"Does anyone have any idea why nodeJS and Async are doing this?"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id103,969
net_rshares46,343,127,228,733
author_curate_reward""
vote details (26)
@kaptainkrayola ·
Wow, the "code block" style for posting looks terrible.  Sorry about that.
👍  , ,
properties (23)
authorkaptainkrayola
permlinkre-kaptainkrayola-does-anyone-have-any-idea-why-nodejs-and-async-are-doing-this-20160714t021007104z
categorynodejs
json_metadata{"tags":["nodejs"]}
created2016-07-14 02:10:06
last_update2016-07-14 02:10:06
depth1
children0
last_payout2016-08-14 03:25:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length74
author_reputation2,518,560,260,114
root_title"Does anyone have any idea why nodeJS and Async are doing this?"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id103,985
net_rshares704,711,977
author_curate_reward""
vote details (3)