简体   繁体   中英

How to return a variable from a Q Node promise?

I have a learning game using Angular, Express, Mongo and Node. I'm now learning how to use promises, so I can wait until something is finished before starting something else.

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.

The user have a master account and can play sessions. Within each session, he will receive a new player . If the user exist and tries to login and his player is logged, he will receive that player back. If the player does not exist, the server will create one for him.

But, I'm having a problem with the variable logged , that says if the user have a 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.

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

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?

If you return a value from a .then() handler, then that value becomes the fulfilled value of the 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:

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() :

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

}

And, then add a .then() handler to the returned promise.

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

Or, in your findUser() function:

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. It appears you can just return true or false from the function directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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