简体   繁体   中英

Converting async/await function to ES5 equivalent

I'm in the process of rewriting an app that executes REST API calls in batches, for example 500 total calls executed 10 at a time. I need assistance in downgrading my js function that uses ES6+ functions to an ES5 equivalent (basically no arrow functions or async/await).

In my original app that was used in environments that supported ES6+ functions (arrow functions, async/await etc.) my working function was as follows:

Original function:

// 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
}

The code I've written so far correctly batches up the rest calls in the first group of 10, for example, but I seem to be getting tripped up in resolving the Promise, so the for-loop can continue iterating.

My work-in-progress re-written function:

//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
    }

My question is, once my 10 restCalls are completed, how can I then properly resolve the Promise and continue to iterate through the for-loop ?

For reference, I've tried compiling the original function using Babel but it wouldn't compile in my Maven app, hence re-writing from scratch.

Without async/await , you cannot pause your for loop. But you could reproduce that behavior by using a recursive function, calling itself after each batch of 10. Something along those lines (not tested) :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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