javascript - NodeJS async library parallel method strange behavior -
javascript - NodeJS async library parallel method strange behavior -
i'm attempting utilize async module nodejs first time, , i'm running problem when seek , dynamically build array of functions async.parallel function run:
methods = []; (key in entries) { methods.push(function(callback) { homecoming callback(null, key); }); } homecoming async.parallel(methods, function(err, results) { console.log(results); homecoming render_views(req, res, 'view_blog_all', { entries: entries }); }); the output maintain seeing is:
[ 'powerful_sms_communication_for_teams', 'powerful_sms_communication_for_teams', 'powerful_sms_communication_for_teams' ] and 'entries' object i'm looping on has 3 different keys. i'm missing?
async module: https://github.com/caolan/async
this mutual problem people run asynchronous logic. key remember callback not run until phone call async.parallel, , @ point, key lastly key value in loop.
one way solve capture key value within new scope using iife.
methods = []; (key in entries) { (function(key){ methods.push(function(callback) { homecoming callback(null, key); }); })(key); } you can utilize async.map normal array , single iterator instead.
return async.map( entries, function(key, callback){ callback(null, key); }, function(err, results) { console.log(results); homecoming render_views(req, res, 'view_blog_all', { entries: entries }); } ); javascript node.js scope
Comments
Post a Comment