无论如何,嵌套的$ http.post总是返回一个promise

时间:2016-12-08 13:20:04

标签: javascript angularjs

第一个$ http.post承诺(当与.then一起使用时)返回一个没有问题的对象,但是当我嵌套另一个$ http.post承诺(也用于.then)时,我永远无法返回一个对象。无论我做什么,它总会回报承诺。

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

1 个答案:

答案 0 :(得分:1)

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

像这样使用:

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

这应该有效!

相关问题