0

I used async module for my project. This is my 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 This is code in file 2. I use function in file 1:

var isexit = db.check(option);
    console.log(isexit);

A problem is console is 'undefined'. If I change code in file 1 to be the 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 will error. So How to return a funciton result in file 1. Pls help. Thanks so much!

1 Answer 1

2

You can't just return it, you'll have to provide a callback for your function:

exports.check = function(option, callback){
  async.parallel([
    //...
  ], function(err, results){
    var object = {};
    //...
    callback(err, object);
  });
}

Then your call will look like:

db.check(option, function(isexit){
  console.log(isexit);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for helping! Because I want to check to call another function. I have many functions. So I must call check function when I want to check different function. So Could you advise for me to solve this problem?. use return .when i don' use async. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.