0

How can we implement synchronized code for following code snippet. We are to write code in call-back function of previous function. Which created callback-hell (as they said). How can we write separately males and females record fetching separately which can execute synchronously. How can we retain the value for console.log(id[0]); in for loop. Please correct me if I am in wrong direction.

var male_arr = []; var female_arr = [];

/*************   Fetching record from database with condition gender=0
***************/ user.find({gender: 0}, {_id: 1}, function (err_male, male) {

    if (err_male) {
        console.log(err_male);
    }

    male_arr = male;    // initializing males array
    looplength = male.length;
    //
    // With CAALBACK function, we have fetched records with condition gender = 1
    //
    user.find({gender: 1}, {_id: 1}, function (err_female, female) {
        if (err_female) {
            console.log(err_female);
        }

        female_arr = female;     // initializing females array

            // Populate the male array
            for (var loop = 0; loop < looplength; loop++) {

                var id = male_arr.splice(0, 1);
     //#1          /*****here is the spliced element********/

                console.log("Outside request api");
                console.log(id[0]);


                // Get all records of females which are in request table of male at id[0]
                request.find({male: id[0]._id}, {female: 1}, function (err, data) {
    //#2           /*****This value of male in id[0] is not retained for match ********/
                    console.log("Inside request api");
                    console.log(id[0]);
                    if (err) {
                        console.log(err);
                    }
                });

            }

    }); });

2 Answers 2

2

Although Caio's answer might fit your problem, might I suggest using Promises?

Using Bluebird you can do something like this:

Note: this is an example and it might not be actually functional.

var Promise = require('bluebird');

var getMaleFn() {
    return new Promise(function(resolve, reject){
        // Make the call to your API.
        // Handle errors, if any, return reject(err);
        return resolve(males);
    });
}

var getFemaleFn() {
    return new Promise(function(resolve, reject){
        // Make the call to your API.
        // Handle errors, if any, return reject(err);
        return resolve(females);
    });
}

var males = [];
var females = [];

// Fetch all males
getMaleFn()
    //Iterate each male and put it in the array
    .map(function(male) {
        males.push(male);
    })
    // Fetch all females
    .then(function(){
        return getFemaleFn()
    })
    //Iterate each females and put it in the array
    .map(function(female) {
        females.push(female);
    })
    .then(function(){
        console.log('done!');
    });
Sign up to request clarification or add additional context in comments.

Comments

1

Async waterfall may be the answer to your problems!

https://github.com/caolan/async#waterfall

Comments

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.