返回函数Javascript

返回函数Javascript

本文介绍了返回函数Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在回复查询时遇到了一些问题。

I have several problems with return of queries.

在这里,我想做什么:

//If the email hasn't a good format
if(email_not_good_format())
   //I do something
else if(email_already_exists_in_mysql(email))
  //I do something


function email_already_exists_in_mysql(email){
connection.query('SELECT COUNT(*) AS nb FROM user WHERE emailUser = ' + connection.escape(email), function(err, rows, fields) {
        if (err) throw err;
        if(rows[0].nb == 0)
            return false;
        else
            return true;
    });
}

我在不同的帖子上看到了回调函数,但它不适用于我我想做。

I saw on different posts callback function but it doesn't work for what I want to do.

推荐答案

是的,你只需要改变你对代码的看法。您应该编写一个名为的函数if_email_already_exists_in_mysql

Yes it does, you just need to change the way you think about code. Instead of writing email_already_exists_in_mysql you should instead write a function called if_email_already_exists_in_mysql:

/* Executes callback if email
 * already exists in mysql:
 */
function if_email_already_exists_in_mysql (email,callback) {
    connection.query(
        'SELECT COUNT(*) AS nb FROM user WHERE emailUser = ' +
        connection.escape(email),

        function(err, rows, fields) {
            if(rows[0].nb != 0) {
                callback();
            }
        }
    )
}

然后不写这个:

//If the email hasn't a good format
if(email_not_good_format()) {
    //I do something
}
else if(email_already_exists_in_mysql(email)) {
    //I do something
}

你写的它改为:

//If the email hasn't a good format
if(email_not_good_format()) {
    //I do something
}
else {if_email_already_exists_in_mysql(email),function(){
    //I do something
})}

现在,您可能会问自己,如果之后还有其他的话怎么办?好吧,你需要修改 if_email_already_exists_in_mysql 函数来表现得像 if ... else 而不是仅仅和 if

Now, you may ask yourself, what if there is another else after that? Well, you need to modify the if_email_already_exists_in_mysql function to behave like and if...else instead of just and if:

function if_email_already_exists_in_mysql (email,callback,else_callback) {
    connection.query(
        'SELECT COUNT(*) AS nb FROM user WHERE emailUser = ' +
        connection.escape(email),

        function(err, rows, fields) {
            if(rows[0].nb != 0) {
                callback();
            }
            else if(else_callback) {
                else_callback();
            }
        }
    )
}

所以您可以这样称呼它:

so that you can call it like this:

//If the email hasn't a good format
if(email_not_good_format()) {
    //I do something
}
else {
    if_email_already_exists_in_mysql(email),function(){
        //I do something
    },
    // else
    function(){
        //I do something else
    }
)}

您可以编写异步代码来执行常规代码只能执行的操作,而不是返回您在回调中传递的值。请记住:

You can write async code to do pretty much anything regular code can do only instead of returning a value you pass in a callback. Remember:

返回同步代码==在异步代码中传递回调

return in synchronous code == passing in callbacks in asynchronous code.

因此代码结构必须不同,但正如我在上面演示的那样,你想要实现的逻辑可以完全相同。

The code structure must therefore be different but as I demonstrated above the logic you want to implement can be exactly the same.

这篇关于返回函数Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 19:46