简体   繁体   English

在nodejs中使用async是一种好习惯吗? 如何在Node.js中使用辅助函数?

[英]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. 我是nodejs的新手,我想创建用于从集合中查找用户的辅助函数。 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. 当我简单地创建一个函数并存储该函数的结果时,控件就是从该位置传递的,而不是等待输出作为Nodejs基本的异步功能。

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);
                        }
                    });
},

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 这是有关回调的文章的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM