为什么我的javascript承诺无法解决?

时间:2018-01-10 06:39:36

标签: javascript node.js promise

exports.validatePasscode = (req,res) => {
    var number = req.body.number || null;
    var passcode = parseInt(req.body.passcode) || null;

    return _users_helper.validatePasscode(number, passcode).then( () =>  {
        return _users_helper.fetchUserByPhone(number)
    }).then(user => {
        console.log("abc-abc", user); 
    }).catch(err => { 
        console.log(err);
    });
}

var fetchUserByPhone = function(number){
    return new Promise(function(resolve, reject){
        return db.ref('/phones-users/' + number).once('value').then(function(snapshot){
            if(snapshot.exists() == false){ return resolve(null); };
            return fetchUserById(snapshot.child('user_id').val());
        });
    });
};
exports.fetchUserByPhone = fetchUserByPhone;


var fetchUserById = function(id){
    return new Promise(function(resolve, reject){
        return db.ref('/users/' + id).once('value').then(function(snapshot){
            if(snapshot.exists()){
                var result = snapshot.val();
                result.id = id;
                console.log("User found result", result); //this prints
                resolve(result);
            }else{
                console.log("User not found");
                resolve(null);
            }
        });
    });
}
exports.fetchUserById = fetchUserById;

运行此代码时,User found result正确打印。但是,abc-abc不打印。为什么呢?

2 个答案:

答案 0 :(得分:1)

这不是新承诺如何运作,通过电话获取用户应该是:

var fetchUserByPhone = function(number){
  return db.ref('/phones-users/' + number)
  .once('value')
  .then(function(snapshot){
    if(snapshot.exists() == false){ 
      return null; 
    };
    return  fetchUserById(snapshot.child('user_id').val());
  });
};

按ID获取用户应为:

var fetchUserById = function(id){
  return db.ref('/users/' + id).once('value')
  .then(function(snapshot){
      if(snapshot.exists()){
          var result = snapshot.val();
          result.id = id;
          console.log("User found result", result); //this prints
          return result;
      }else{
          console.log("User not found");
          return null;
      }
  });
}

当您调用已经返回类似承诺的函数时,您不需要创建新的承诺。

答案 1 :(得分:0)

fetchUserByPhone返回的承诺未得到解决/拒绝。

由于您的db方法仍然返回一个promise,因此没有必要创建一个Promise包装器。可能的替代方案:



exports.validatePasscode = (req, res) => {
  var number = req.body.number || null;
  var passcode = parseInt(req.body.passcode) || null;

  return _users_helper.validatePasscode(number, passcode).then(() => {
    return _users_helper.fetchUserByPhone(number)
  }).then(user => {
    console.log("abc-abc", user);
  }).catch(err => {
    console.log(err);
  });
}

var fetchUserByPhone = function(number) {

  return db.ref('/phones-users/' + number).once('value').then(function(snapshot) {
    if (snapshot.exists() == false) {
      return Promise.reject(null);
    };
    return fetchUserById(snapshot.child('user_id').val());
  });
};
exports.fetchUserByPhone = fetchUserByPhone;


var fetchUserById = function(id) {
  return db.ref('/users/' + id).once('value').then(function(snapshot) {
    if (snapshot.exists()) {
      var result = snapshot.val();
      result.id = id;
      console.log("User found result", result); //this prints
      return result;
    } else {
      console.log("User not found");
      return Promise.reject(null);
    }
  });
}
exports.fetchUserById = fetchUserById;