将异步/等待功能转换为等效于ES5

时间:2020-02-28 23:13:48

标签: javascript maven

我正在重写一个应用程序,该应用程序分批执行REST API调用,例如一次执行10个,总共执行500个调用。在将使用ES6 +函数的js函数降级为等效于ES5的js函数时,我需要帮助(基本上没有箭头函数或异步/等待)。

在支持ES6 +功能(箭头功能,异步/等待等)的环境中使用的原始应用程序中,我的工作功能如下:

原始功能

// Async function to process rest calls in batches
const searchIssues = async (restCalls, batchSize, loadingText) => {
    const restCallsLength = restCalls.length;
    var issues = [];
    for (let i = 0; i < restCallsLength; i += batchSize) {
        //create batch of requests
        var requests = restCalls.slice(i, i + batchSize).map((restCall) => {
            return fetch(restCall)
                .then(function(fieldResponse) {
                    return fieldResponse.json()
                })
                .then(d => {
                    response = d.issues;

                    //for each issue in respose, push to issues array
                    response.forEach(issue => {
                        issue.fields.key = issue.key
                        issues.push(issue.fields)
                    });
                })
        })
        // await will force current batch to resolve, then start the next iteration.
        await Promise.all(requests)
            .catch(e => console.log(`Error in processing batch ${i} - ${e}`)) // Catch the error.

        //update loading text
        d3.selectAll(".loading-text")
            .text(loadingText + ": " + parseInt((i / restCallsLength) * 100) + "%")

    }

    //loading is done, set to 100%
    d3.selectAll(".loading-text")
        .text(loadingText + ": 100%")
    return issues
}

例如,到目前为止,我已经正确地编写了代码,将其余的调用在第一个10个组中进行了批处理,但是在解决Promise方面我似乎陷入了困境,因此for循环可以继续迭代。 / p>

我的在制品重写功能:

//Async function process rest calls in batches
    function searchIssues(restCalls, batchSize, loadingText) {
        const restCallsLength = restCalls.length;
        var issues = [];
        for (var i = 0; i < restCallsLength; i += batchSize) {
            //create batch of requests
            var requests = restCalls.slice(i, i + batchSize).map(function(restCall) {
                    return fetch(restCall)
                        .then(function(fieldResponse) {
                            return fieldResponse.json()
                        })
                        .then(function(data) {
                            console.log(data)
                            response = data.issues;

                            //for each issue in respose, push to issues array
                            response.forEach(function(issue) {
                                issue.fields.key = issue.key
                                issues.push(issue.fields)
                            });
                        })
                })
                //await will force current batch to resolve, then start the next iteration.
            return Promise.resolve().then(function() {
                console.log(i)
                return Promise.all(requests);
            }).then(function() {
                d3.selectAll(".loading-text")
                    .text(loadingText + ": " + parseInt((i / restCallsLength) * 100) + "%")
            });
            //.catch(e => console.log(`Error in processing batch ${i} - ${e}`)) // Catch the error.
        }

         //loading is done, set to 100%
         d3.selectAll(".loading-text")
             .text(loadingText + ": 100%")
         return issues
    }

我的问题是,完成10个restCall之后,如何正确解决Promise并继续遍历for循环

作为参考,我尝试使用Babel编译原始函数,但是无法在我的Maven应用程序中进行编译,因此从头开始重写。

1 个答案:

答案 0 :(得分:1)

如果没有async/await,则无法暂停for循环。但是您可以通过使用递归函数来重现该行为,在每批10之后调用它自己。类似于(未测试)

// Async function to process rest calls in batches
function searchIssues(restCalls, batchSize, loadingText) {
  var restCallsLength = restCalls.length,
      issues = [],
      i = 0;

  return new Promise(function(resolve, reject) {
    (function loop() {
      if (i < restCallsLength) {
        var requests = restCalls
          .slice(i, i + batchSize)
          .map(function(restCall) {
            return fetch(restCall)
              .then(function(fieldResponse) {
                return fieldResponse.json();
              })
              .then(function(d) {
                var response = d.issues;

                //for each issue in respose, push to issues array
                response.forEach(issue => {
                  issue.fields.key = issue.key;
                  issues.push(issue.fields);
                });
              });
          });

        return Promise.all(requests)
          .catch(function(e) {
            console.log(`Error in processing batch ${i} - ${e}`);
          })
          .then(function() {
            // No matter if it failed or not, go to next iteration
            d3.selectAll(".loading-text").text(
              loadingText + ": " + parseInt((i / restCallsLength) * 100) + "%"
            );
            i += batchSize;
            loop();
          });
      } else {
        // loading is done, set to 100%
        d3.selectAll(".loading-text").text(loadingText + ": 100%");
        resolve(issues); // Resolve the outer promise
      }
    })();
  });
}