2016-12-08 3 views
0

첫 번째 $ http.post 약속 (.then과 함께 사용하는 경우) 개체를 아무 문제없이 반환하지만 다른 $ http.post 약속을 중첩 할 때도 함께 사용됩니다. 그렇다면 객체를 반환 할 수 없습니다. 내가하는 일에 상관없이 항상 약속을 되풀이합니다. 이 같은

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

콘솔에 오류가 있습니까? –

+0

아니요 오류가 없습니다 – RichC

답변

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

이 작동합니다!

+0

예 - 고정되어 있습니다. 내가 그것을하려고하는 것처럼 내가 그것을 할 수없는 이유가 있습니까? 논리적으로 보이지 않습니다. – RichC

+1

getDocumentFormFields()는 비동기 호출을 생성하고 즉시 약속을 반환합니다. 그것은 반환하기 전에 실행 블록과 다음 블록에 대한 약속을 기다리지 않습니다! * then *을 함수 호출에 추가하면 응답에 의존하는 코드가 실행되기 전에 약속이 해결됩니다. – sledsworth

관련 문제