Richard.Davenport Richard.Davenport - 13 days ago 5
Javascript Question

Passing parameters to promise's success callback in angularjs $q

I realize this is a very similar question to this one. But I'm still unclear on how to do it in my situation. Just need some help with a successful callback.

This is what works:

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

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

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


This is what I want to accomplish:

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;
}

Answer

You can use this:

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

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