node.js - How to return a result of async function (async module) in nodeJs -
i used async module project. code in file 1
exports.check = function(option){ var object = {}; async.parallel([ function(callback){ if(!option.one){callback(null, true);}else{ callback(null, false);} }, function(callback){ if(!option.two){callback(null, true);}else{ callback(null, false);} }, function(callback){ if(!option.three){callback(null, true);}else{ callback(null, false);} } ], function(err, results){ if(results[0] && results[1] && results[2] ){ object.one = results[0]; object.two = results[1]; object.three = results[2]; object.total = true; }else{ object.one = results[0]; object.two = results[1]; object.three = results[2]; object.total = false; } }) return object; }
and code in file 2. use function in file 1:
var isexit = db.check(option); console.log(isexit);
a problem console 'undefined'. if change code in file 1 line (return object):
exports.check = function(option){ var object = {}; async.parallel([ function(callback){ if(!option.one){callback(null, true);}else{ callback(null, false);} }, function(callback){ if(!option.two){callback(null, true);}else{ callback(null, false);} }, function(callback){ if(!option.three){callback(null, true);}else{ callback(null, false);} } ], function(err, results){ if(results[0] && results[1] && results[2] ){ object.one = results[0]; object.two = results[1]; object.three = results[2]; object.total = true; }else{ object.one = results[0]; object.two = results[1]; object.three = results[2]; object.total = false; } return object; }) }
it error. how return funciton result in file 1. pls help. much!
you can't return it, you'll have provide callback function:
exports.check = function(option, callback){ async.parallel([ //... ], function(err, results){ var object = {}; //... callback(err, object); }); }
then call like:
db.check(option, function(isexit){ console.log(isexit); });
Comments
Post a Comment