将参数传递给promisejs $ q中的promise成功回调

时间:2015-09-08 18:52:40

标签: javascript angularjs q

我意识到这是与one非常相似的问题。但在我的情况下,我仍然不清楚如何做到这一点。只需要一些成功回调的帮助。

这是有效的:

function getStuff(accountNumber) {
    var logMessage = 'POST GetStuff';

    return $http.post(GetStuff, { custId: accountNumber })
        .then(log);
}

function log(response) {
    logger.debug(response);
    return response;
}

这就是我想要完成的事情:

function getStuff(accountNumber) {
    var logMessage = 'POST GetStuff';

    return $http.post(GetStuff, { custId: accountNumber })
        .then(log(response, logMessage);
}

function log(response, logMessage) {
    logger.debug(logMessage, response);
    return response;
}

2 个答案:

答案 0 :(得分:8)

您可以使用:

function getStuff(accountNumber) {
    var logMessage = 'POST GetStuff';

    return $http.post(GetStuff, { custId: accountNumber })
        .then(
           function success(response) {
               return log(response, logMessage);
           }
        );
}

答案 1 :(得分:0)

看看它是否可以帮到你:

function getStuff(accountNumber) {
  var logMessage = 'POST GetStuff';
  var deferred = $q.defer();

  return $http.post(GetStuff, { custId: accountNumber })
   .success(function(response){
      deferred.resolve(response);
      log(response, logMessage);
   }).error(function(){
      deferred.reject();
   })     
   return deferred.promise;
}