0

I have three operations to do one after another

1.Fetch some rows from db

2.Another mysql query in forloop for getting some data and store in variable

3.Display the data

For that i am using async waterfall method.

async.waterfall([
            function(callback){
                //first sql and pass the result to second function
                collection.getAllCollections(function (status,error,result) {
                    callback(null,result);
                });
            },
           //running another query in loop with result with previous
            function(result, callback){
                for(var i=0;i<result.length;i++){
                    collection.getImages(result[i].id,function (status,error,user) {
                        //append the query result to old result
                         result[i]['users'] = user;
                    });
                }
                callback(null,result);
            }
        ], function (err, result) {

            console.log("result",result);
        });

But the problem final result does not contains the user results because the second query(query in for loop is asynchronous)

1 Answer 1

1

You realised the problem at hand. Your callback basically has to wait for the for loop to end. For example like this:

async.waterfall([
    function(next){
        //first sql and pass the result to second function
        collection.getAllCollections(function (status,error,result) {
            next(null, result);
        });
    },
    function(result, next){
        var calls = [];

        //putting every call in an array
        result.forEach(function(resultObject){
            calls.push(function(callback) {
                collection.getImages(resultObject.id, function (status, error, user) {
                     resultObject['users'] = user;
                     callback(null, resultObject);
                });
            }
        )});

        //performing calls async parallel to each other
        async.parallel(calls, function(err, results) {
            //executed as soon as all calls are finished
            if (err) {
                next(err, null);
            } else {
                next(null, results);
            }
        });
    }
], function (err, result) {

    console.log("result",result);
});

Documentation: http://caolan.github.io/async/docs.html#parallel

Sign up to request clarification or add additional context in comments.

5 Comments

But where can put isDone function?? i am using async waterfall method
@Jabaa I updated my code to clarify my solution. This should work but I didn't test it.
@Jabaa I updated my answer again. There is a better, cleaner and more efficient solution with async.parallel available. That should get you in the right direction.
Thanks let me try it
@Jabaa please accept my answer if it works for you :)

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.