【问题标题】:Calling APIs in a loop - Angular js循环调用 API - Angular js
【发布时间】:2017-11-24 05:33:49
【问题描述】:

我有一组 API 必须在 for 循环中运行

for(var i=0; i<array.length; i++ )
    service.getfunction(array[i]).then(function(response) {
      service.getfunction1(response).then(function(response) {
        service.getfunction2(response).then(function(response) {
          service.getfunction3(response).then(function(response) {
             console.log(response);
          });
        });
      });
    });
)

只有在我从最后一个 getfunction3 API 获得第一个循环的结果后,才应该开始第二个循环。我该怎么做?

【问题讨论】:

    标签: javascript angularjs api for-loop promise


    【解决方案1】:

    首先 - 你可以像这样链接你的承诺:

    function doJob(i) {
      return service.getfunction(array[i]).then(function(response) {
        return service.getfunction1(response);
      }).then(function(response) {
        return service.getfunction2(response);
      }).then(function(response) {
        return service.getfunction3(response);
      }).then(function(response) {
        console.log(response);
      });
    }
    

    此函数将返回承诺,一旦所有此服务调用完成,该承诺将被解决。 现在让我们使用它:

    var p = Promise.resolve(true);
    for(var i = 0; i < array.length; i++) {
      (function(idx) { //We need to wrap it or we will pass incorrect i into doJob
        p = p.then(function() {
          return doJob(idx);
        });
      })(i);
    }
    p.then(function() {
      console.log('Everything done');
    });
    

    【讨论】:

    • 这可以通过省略return service.getfunctionX(response) 来简化很多。 then 默认会返回上一个 promise 的解析值,因此您可以简单地编写 service.getFunction(array[i]).then(service.getfunction1).then(service.getfunction2).then(service.getfunction3)...
    • 如果任何 API 出现故障怎么办?
    • @Anna 你可以使用then 函数的第二个参数来处理错误,或者链接catch 函数。在最后一个catch 之后,它将从整个链中获取所有错误。
    【解决方案2】:

    将其包装在一个函数中:

    // Declare function 
    function LoopAPI() {
        for(var i=0; i<array.length; i++ )
            service.getfunction(array[i]).then(function(response) {
              service.getfunction1(response).then(function(response) {
                service.getfunction2(response).then(function(response) {
                  service.getfunction3(response).then(function(response) {
                     console.log(response);
    
                     // Call function again
                     LoopAPI();
                  });
                });
              });
            });
        )
    }
    
    // First call
    LoopAPI();
    

    【讨论】:

      【解决方案3】:

      您可以链接承诺,然后使用 Array.reduce 依次调用它们:

      array.reduce(function(previous, next)
      {
          return previous.then(function()
          {
              return service.getfunction(next);
          })
          .then(function(response)
          {
              return service.getfunction1(response);
          })
          .then(function(response)
          {
              return service.getfunction2(response);
          })
          .then(function(response)
          {
              return service.getfunction3(response);
          });
      }, Promise.resolve())
      .then(function()
      {
          console.log("All done.");
      })
      .catch(function(error)
      {
          console.log(error);
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-27
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-23
        相关资源
        最近更新 更多