Nirav Majethiya
Nirav Majethiya

Reputation: 31

Is it good practise to use async in nodejs ? How to use helper function in nodejs?

I am new to nodejs and I want to create helper function for finding user from collection. I want to excute that helper function before adding any new document into the database. Parallaly I want to use that helper function on multiple times in other places.

When I simply create one function and store result of that function, the control is passes from that place and doesn't wait for output as Nodejs basic Async functionality.

How can I customise my code to use helper function and wait for the result and then perform required operation

How to use nested callback ? Nested callback isn't waiting for callback data and control goes to next line

Here is my code :

login: function (req, res) {

    var user = helpers.checkIfDataExists(req.body.mobileNumber, function (user) {
    if (user) {
       console.log('user exists');
       var email = helpers.checkIfDataExists(req.body.email, function (email) {
          if (email) {
             console.log('email exists');
          } else {
             console.log('email not exists');
          }
      });
    } else {
       console.log('user not exists')
    }
   });
}

I want that control waits till function is executed and then condition will get executed. Here control goes by default in else part everytime.

Here is code of helper function :

checkIfDataExists: function (value, callback) {
    User.find({mobileNumber: value})
                    .exec(function (err, result) {
                        if (err) {
                            console.log('Error occured');
                            callback(false);
                        } else {
                            console.log('result found');
                            callback(result);
                        }
                    });
},

Upvotes: 0

Views: 124

Answers (1)

Arpit Solanki
Arpit Solanki

Reputation: 9931

Use callbacks. Pass a callback function to your helper function in which the results will be returned and then in your helper function write a call to that function when your computations are over.

checkIfDataExists: function (value, callback) {
    User.find({mobileNumber: value})
                    .exec(function (err, result) {
                        if (err) {
                            console.log('Error occured');
                            callback(false);
                        } else if (!result) {
                            console.log('Not Found');
                            callback(false);
                        } else {
                            console.log('result found');
                            callback(result);
                        }
                    });
},

then use something like this in your main function.

signUp: function (req, res) {

    var user = helpers.checkIfDataExists(req.body.mobileNumber, function(user){

// do something with results here
log(user);
    if (!user) {
        // add new user
    } else {
        res.send("User exists");
    }
});
}

here is a link to article about callbacks

Upvotes: 0

Related Questions