javascript - Node async callback being called for operations complete -
i trying use async library in order deal asynchronous functions, , unable functionality need.
the basic idea building json object using multiple calls redis database. want return json object after redis calls have been completed, tried using async.parallel
don't seem understand properly.
my callback called empty json object , console logs within functions seen.
here code:
server.js:
gethostobject(redis,sess.hostname,function(data) { console.log('calledback data: '+json.stringify(data)); });
gethostobject.js
var async = require('async'); var gethostobject = function(redis, hostname, callback) { var hostobject = { hostname: hostname, pushers: [], tracklist: [] }; var getsongobject = function(err,song) { console.log('song got!'); hostobject.tracklist.push(song); }; var getsongs = function() { redis.lrange(hostname+":songs",0,-1, function(err,data) { if (err) {console.log('error reading songs! '+hostname+'\n'+err);} for(var i=0;i<data.length;i++) { redis.hgetall(hostname+":song:"+data[i], getsongobject); } }); }; var getpusherobject = function(err,pusher) { console.log('pusher got!'); hostobject.pushers.push(pusher); }; var getpushers = function() { redis.smembers(hostname+":pushers", function(err, data) { if (err) {console.log('error reading pushers! '+hostname+'\n'+err);} for(var i=0;i<data.length;i++) { redis.hgetall(hostname+":pusher:"+data[i], getpusherobject); } }); }; async.parallel([ function() { getpushers(); }, function() { getsongs(); } ],callback(hostobject)); }; module.exports = gethostobject;
console output:
calledback data: {"hostname":"47b71","pushers":[],"tracklist":[]} pusher got! song got!
first of all, inside gethostobject
function, in async.parallel
call, passing callback(hostobject)
. evaluated before async code executed, that's why hostobject
it's empty.
aside that, async.parallel needs know when individual tasks completed. that's it, should call task callback, following error, result
convention. take @ the example in docs.
you need change getpushers
, getsongs
use callbacks, pass results callbacks, , compose hostobject
in parallel complete cb.
im trying illustrate here based on code, need modify make work.
var getsongs = function(cb) { redis.lrange(hostname+":songs",0,-1, function(err,data) { if (err) {console.log('error reading songs! '+hostname+'\n'+err);} for(var i=0;i<data.length;i++) { redis.hgetall(hostname+":song:"+data[i], getsongobject); } // tracklistdata should tracklist array, need build first. cb(null, tracklistdata); }); }; var getpushers = function(cb) { redis.smembers(hostname+":pushers", function(err, data) { if (err) {console.log('error reading pushers! '+hostname+'\n'+err);} for(var i=0;i<data.length;i++) { redis.hgetall(hostname+":pusher:"+data[i], getpusherobject); } // pushersdata should pushers array, need build first cb(null, pushersdata); }); }; async.parallel(getsongs, getpushers, function(err, results){ if(err) // err handling callback({ hostname: hostname, pushers: results[1], tracklist: results[0] }); });
when comment need build array first, mean looks might want use async.parallel
inside each of functions when build array. maybe
var getpushers = function(cb) { redis.smembers(hostname+":pushers", function(err, data) { var getoperations = []; if (err) {console.log('error reading pushers! '+hostname+'\n'+err);} for(var i=0;i<data.length;i++) { getoperations.push((function(pusher){ return function(callback){ redis.hgetall(hostname+":pusher:"+pusher, callback); } })(data[i])); } async.parallel(getoperations, function(err, pushers){ cb(null, pushers); }); }); };
Comments
Post a Comment