简体   繁体   English

$ http.post在另一个$ http.post中的调用

[英]$http.post call inside another $http.post

I want to make an $http.post inside another $http.post as the second one is dependant on the first. 我想在另一个$http.post $http.post一个$http.post因为第二个依赖于第一个。 Basically what I'm doing is: 基本上我正在做的是:

$http.post("/my/server/location").then(function (response) {
    $http.post("/my/second/api/call/"+response.data.number).then(function (response) {
      $scope.message = "Created successfully";
    }, function (response){
      $scope.message = "Could not create";
    });

    //Create modal from response.data received from first API Call
    //Add $scope.message to this modal.
    });

What's happening as you would have guessed is the second one stays pending until the initial promise is returned, and I want to show $scope.message in the modal which is hence, not possible. 您可能会想,正在发生的事情是第二个待定,直到返回初始诺言为止,我想在模式中显示$scope.message ,因此是不可能的。 While I understand why that's happening, I can't seem to figure how to get around that. 虽然我知道为什么会发生这种情况,但似乎无法弄清楚该如何解决。 I tried dealing with $q but made a mess out of it. 我尝试处理$q但搞砸了。 Any help will be gladly appreciated. 任何帮助将不胜感激。

To chain the message use a return statement in both the success handler and the rejection handler of the second XHR: 链接消息,请在第二个XHR的成功处理程序和拒绝处理程序中使用return语句

$http.post("/my/server/location").then(function (response1) {
    return $http.post("/my/second/api/call/"+response1.data.number)
      .then(function (response2) {
        $scope.message = "Created successfully";
        return $scope.message;
    }).catch(function (errorResponse2) {
        $scope.message = "Could not create";
        return $scope.message;
    }).then(function(message) {

        //Create modal from response1.data received from first API Call
        //Add `message` from the second XHR to this modal.

        return [response1.data, message];
    });
});

The return statement in the .catch rejection handler converts the rejected promise to a success which is handled by the next .then method in the chain. .catch拒绝处理程序中的return语句将被拒绝的承诺转换为成功,该成功由链中的下一个.then方法处理。

The final promise returns an array which has both the data from the first XHR and the message from the second XHR. 最终的promise返回一个数组,该数组同时包含来自第一个XHR的数据和来自第二个XHR的消息。

Chaining Promises 连锁承诺

Because calling the .then method of a promise returns a new derived promise, it is easily possible to create a chain of promises. 因为调用promise的.then方法会返回新的派生promise,所以很容易创建promise的链。

It is possible to create chains of any length and since a promise can be resolved with another promise (which will defer its resolution further), it is possible to pause/defer resolution of the promises at any point in the chain. 可以创建任意长度的链,并且由于一个承诺可以用另一个承诺来解决 (这将进一步推迟其解决方案),因此可以在链中的任何点暂停/推迟对承诺的解决。 This makes it possible to implement powerful APIs. 这样就可以实现功能强大的API。

— AngularJS $q Service API Reference - Chaining Promises — AngularJS $ q服务API参考-链式承诺

You're right, $q won't help here unless you want both promises to be executed at once and then wait for both of their responses. 没错, $q在这里无济于事,除非您希望立即执行两个promise,然后等待它们的两个响应。

Here you want to execute one promise then the other, which according to your code looks fine. 在这里,您要执行一个承诺,然后执行另一个承诺,根据您的代码,它看起来不错。 However, anything you want to do after the second promise has executed needs to be done in that .then() block. 但是,在执行第二个Promise之后,您想要执行的任何操作都需要在该.then()块中完成。

Example: 例:

$http.post("/my/server/location").then(function (response) {
    $http.post("/my/second/api/call/"+response.data.number).then(function (response) {
       $scope.message = "Created successfully";
       // Create modal from response.data received from first API Call
    }, function (error){
        $scope.message = "Could not create";
    });
});
$http.post("/my/server/location")
 .then(function (response) {

    $http.post("/my/second/api/call/"+response.data.number).then(function 
      (response) {

     }, function (response){

     });

    $scope.message = "Created successfully";

     //Create modal from response.data received from first API Call
    //Add $scope.message to this modal.
    $scope.buildModal(response.data);

})
.catch(function(error){
    $scope.message = "Could not create";
    $scope.buildModal();
})

So the issue with what you had was that $scope.message = "Created successfully"; 因此,您遇到的问题是$scope.message = "Created successfully"; was locked in the nested api call. 被锁定在嵌套的api调用中。 By taking it out of that you can build your modal immediately after the first api call. 通过消除这种情况,您可以在第一次api调用后立即构建模态。 In the code above, nothing is waiting for the second api call to finish because nothing is in its .then block 在上面的代码中,没有任何内容等待第二个api调用完成,因为其.then块中没有任何内容

I moved the modal building to a separate function buildModal because it has to be called both from the .then and the .catch block 让我感动的模式建设,以一个单独的函数buildModal ,因为它必须从两个被叫.then.catch

Try this 尝试这个

 var fn = { test: function() { return $http.post("/my/server/location"); } }; fn.test().success(function(response) { $http.post("/my/second/api/call/"+response.data.number).then(function (response) { $scope.message = "Created successfully"; }, function (response){ $scope.message = "Could not create"; } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM