1

I have this method in one of my service to make API calls:

this.postData = function(requestURL, requestObj) {
    var deferred = $q.defer();
    console.log("Reached APIService @POST", requestURL);
    $http.post(requestURL, requestObj).success(
        function(data, status, headers, config, statusText) {
            deferred.resolve(data, status, headers, config, statusText);
            console.log(status);
        }).error(
        function(data, status, headers, config, statusText) {
            deferred.reject(data, status, headers, config, statusText);
            console.log(status);
        });
    return deferred.promise;
};

Basically this worked fine, but recently I needed headers data in my code to fetch the error messages in case of exceptions. I am confused how to get that info in returned promise. The above function when called returns only data and rest 4 items are undefined. I believe promise cannot resolve multiple items as above.

Then how do I returned the object in promise as to get the entire information of object returned by API. (As document says the response contains 5 fields, data, status, headers, config, statusText).

Need help..

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Saurabh Tiwari
  • 4,632
  • 9
  • 42
  • 82
  • before resolving or rejecting the promise, have you tried to log out the other parameters (data, status, headers, config, statusText)? Do they log out correctly? – charliebrownie Nov 27 '15 at 18:05
  • I am logging out data and status, and yes they print out fine in this service method. – Saurabh Tiwari Nov 27 '15 at 18:09
  • 1
    Have a look at @Thomas solution, that's the good-way to go: `$http.post` already returns a promise itself so you should avoid `$q.defer()` - also known as the [**deferred antipattern**](http://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) – charliebrownie Nov 27 '15 at 18:25

1 Answers1

2

Promises can only resolve to one value, not five, so the remaining arguments you pass to resolve are silently dropped.

The good news is that $http.post() itself already returns a promise, so you can just do:

this.postData = function (requestURL, requestObj) {
    console.log("Reached APIService @POST", requestURL);
    return $http.post(requestURL, requestObj).then(
        function (response) {
            console.log(response.status);
            return response;
        }),
        function (response) {
            console.log(response.status);
            throw response;
        });
};

Or, without the logging:

this.postData = function (requestURL, requestObj) {
    return $http.post(requestURL, requestObj);
};

The response object has properties data, status, headers, and so on. Documentation.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Thomas
  • 174,939
  • 50
  • 355
  • 478
  • I missed that `$http.post` already returned a `promise`. You are right, your solution is the best one, avoiding the `Deferred antipattern`. – charliebrownie Nov 27 '15 at 18:21