简体   繁体   English

如何从Q Node Promise返回变量?

[英]How to return a variable from a Q Node promise?

I have a learning game using Angular, Express, Mongo and Node. 我有一个使用Angular,Express,Mongo和Node的学习游戏。 I'm now learning how to use promises, so I can wait until something is finished before starting something else. 我现在正在学习如何使用Promise,因此我可以等到某些事情完成后再开始其他事情。

Right now, I'm trying to wait until the database says if the user trying to login already have an account. 现在,我正在等待数据库提示您是否要登录的用户已经有一个帐户。 I've succeeded with that, so if the user does not exist, the .then() will call the onReject function that will create the user. 我已经成功了,所以如果用户不存在, onReject .then()将调用onReject函数来创建用户。

The user have a master account and can play sessions. user具有主帐户,可以播放会话。 Within each session, he will receive a new player . 在每个会话中,他将收到一个新player If the user exist and tries to login and his player is logged, he will receive that player back. 如果该user存在并尝试登录并且其player已登录,则他将重新收到该player If the player does not exist, the server will create one for him. 如果player器不存在,服务器将为他创建一个。

But, I'm having a problem with the variable logged , that says if the user have a logged player . 但是,我对logged的变量有问题,即用户是否有记录的player On the second line of the function findUser , I'm creating a variable called logged and I'm trying to return it, so I can use on the onResolve function. 在功能上的第二行findUser ,我创建了一个名为变量logged ,我试图返回它,这样我就可以在使用onResolve功能。

UserModel.find(
            { username: username, password: password},
            findUser ).then( 
             onResolve,
             onReject     );

On Promise resolve 承诺解决

      var onResolve = function (logged) {

          if (logged) { 
             //RETURN USER'S PLAYER
          } else {
             //CREATE USER'S PLAYER
          }

       }

On Promise reject 兑现承诺

        var onReject = function () {
            userModel.save( function (err) {
                if (!err) {
                    //CREATE USER
                    //CREATE USER'S PLAYER
                }
            });
        });

Function findUser 函数findUser

var findUser = function (err, data) {
    var logged = false;
    var userSearched = Q.defer();
    if (data.length) { //IF USER IS IN DATABASE
        logged = true;
        userSearched.resolve(logged);
    } else {
        userSearched.reject(logged);
    }

    return userSearched.promise;
};

How could I return the logged variable from the promise to use anywhere I need? 如何从promise中返回logged变量以在我需要的任何地方使用?

If you return a value from a .then() handler, then that value becomes the fulfilled value of the promise. 如果从.then()处理程序返回一个值,则该值将成为promise的已实现值。

 var onResolve = function (logged) {

      if (logged) { 
         //RETURN USER'S PLAYER
      } else {
         //CREATE USER'S PLAYER
      }
      // ** add this **
      return logged;
   }

You can then use it in subsequent .then() handlers: 然后,您可以在后续的.then()处理程序中使用它:

UserModel.find({ username: username, password: password}, findUser)
   .then( onResolve, onReject)
   .then(function(result) {
        // can access result here
    });

If this code is inside a function, then you can return the promise from UserModel.find() : 如果此代码在函数内部,则可以从UserModel.find()返回UserModel.find()

function someFunc() {
    return UserModel.find({ username: username, password: password}, findUser)
       .then( onResolve, onReject);

}

And, then add a .then() handler to the returned promise. 然后,将.then()处理函数添加到返回的Promise中。

someFunc().then(function(result) {
   // access result here
});

Or, in your findUser() function: 或者,在您的findUser()函数中:

var findUser = function (err, data) {
    var userSearched = Q.defer();
    if (data.length) { //IF USER IS IN DATABASE
        userSearched.resolve(true);
    } else {
        userSearched.reject(false);
    }

    return userSearched.promise;
};

findUser.then(function(val) {
    // access resolved value here
}, function(err) {
    // access rejected reason here
});

Though, findUser() does not look asynchronous so I don't really understand why you're using a promise with it at all. 虽然, findUser()看起来并不异步,所以我真的不明白为什么您对它使用了promise。 It appears you can just return true or false from the function directly. 看来您可以直接从函数中返回truefalse

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

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