RichC
RichC

Reputation: 7879

Nested $http.post always returns a promise no matter what

The first $http.post promise (when used with .then) returns an object no problem, but when I nest another $http.post promise (also used with .then) I can never get an object returned. It always returns a promise no matter what I do.

function getDocumentPages($http) {
 var data = { fileName: '@fileNameUrlSafe' };
 return $http.post(controllerBaseUrl + "GetDocumentPages", data)
 .then(function successCallback(response) {
    // =======================================
    // THE LINE BELOW ALWAYS RETURNS A PROMISE
    // =======================================
    var fieldPages = getDocumentFormFields($http);
    var tempModel = {
      pages: response.data,
      fieldPages: fieldPages
    };
    return tempModel;
  }, function errorCallback(response) {
   console.log(response);
 });
}

function getDocumentFormFields($http) {
 var data = { fileName: '@fileNameUrlSafe' }
 return $http.post(controllerBaseUrl + "GetDocumentFormFields", data)
 .then(function successCallback(response) {
   return response.data;
 }, function errorCallback(response) {
   console.log(response);
 });
}

Upvotes: 0

Views: 71

Answers (1)

sledsworth
sledsworth

Reputation: 143

function getDocumentPages($http) {
var data = { fileName: '@fileNameUrlSafe' };
return $http.post(controllerBaseUrl + "GetDocumentPages", data)
    .then(function successCallback(response) {

        // =======================================
        // THE LINE BELOW ALWAYS RETURNS A PROMISE
        // =======================================
        return getDocumentFormFields($http).then(function(fieldPages) {
          var tempModel = {
            pages: response.data,
            fieldPages: fieldPages
          };
          return tempModel;
        });

    }, function errorCallback(response) {
        console.log(response);
    });
}

Use like this:

getDocumentPages($http).then(function(response) {
    //Do something with the response
    console.log(response);
});

This should work!

Upvotes: 1

Related Questions