繁体   English   中英

为什么这个嵌套的promise.then在promise的方法之前执行?

[英]Why is this nested promise.then executed before the promise's method?

我试图使用promises调用getLoginState然后存储该值,以便我以后可以使用它。

我想知道为什么在下面的代码块中,在getLoginState方法之前调用q.fcall(getLoginState)中的.then?

var mysql = require('mysql');
var q = require('q');

var login = "tyler";
var connection = mysql.createConnection({
    host : 'localhost',
    user: 'root',
    password: 'root',
    port: '3306',
    database: 'root'
});

var gotLoginState;
var state;

connection.connect(function(err) {
    if(err != null){
        console.log("connection err: "+err);
    }

    q.nfcall(connection.query.bind(connection),"SELECT id, password, salt, gender, banned, gm, pin, pic, characterslots, tos FROM accounts WHERE name = ?",[login])
        .then(function (results) {
          console.log("select: "+results[0][0]);
//        }).then(q.fcall(getLoginState), function () {
            q.fcall(getLoginState)
                .then(function() {
                    console.log("gotLoginState: " + state);
                }).catch(function (error){
                   console.log("error in inner thing");
            }).done();
        }).catch(function (error) {
            console.error("promise error: "+error);
        })
        .done();
});

var accId = 1;
var getLoginState = function() {
    q.nfcall(connection.query.bind(connection), "SELECT loggedin, lastlogin, UNIX_TIMESTAMP(birthday) as birthday FROM accounts WHERE id = ?", [accId])
        .then(function (results) {
            state = results[0][0].loggedin;
        }).catch(function (error) {
            console.log("error in chaining: "+error);
        }).done();
};

promises中的流控制就像在同步代码中一样:

  • 要返回,请使用return关键字。
  • 要发出错误信号,请使用throw关键字。

承诺的工作方式 - 等待他们。 nfcall样式的“errback”回调中调用nfcall 为了使代码工作,您需要从getLoginState返回 ,然后不使用nfcall因为该方法已经返回一个promise:

var getLoginState = function() { // this should be nodeify probably
    return q.nfcall(...) // note the `return` statement here
        .then(function (results) {
            state = results[0][0].loggedin; // do not mutate state like this
        }).catch(function (error) {         // instead use return values
            console.log("error in chaining: "+error); // don't suppress errors
                                                      // logging is not enough
        }); // don't do `.done` if you want to chain to it
};

然后在上面的部分:

// don't use the `.connect` callback, promisify it
q.nfcall(...) // promisify that method _once_ in your real code
.then(function (results) {
   console.log("select: "+results[0][0]); // why just the log?
   return getLoginState() // note this is a regular function call
            .then(function() { // also note the `return`
                console.log("gotLoginState: " + state);
            }).catch(function (error){
               console.log("error in inner thing");
    }); // no done here, since we chain to it
}).catch(function (error) {
    console.error("promise error: "+error);
}).done(); // you only need one `.done` at the end, and only in old
           // promise libraries

我想强调的是,这可以更好地编写,没有充分的理由在这里嵌套而不是链,并且连接应该在一个承诺中完成 - 上面的代码就是最接近你的代码的代码。

暂无
暂无

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

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