Javascript [SOLVED]: Perform a set of recursive HTTP GET calls, and wait for them all

Javascript [SOLVED]: Perform a set of recursive HTTP GET calls, and wait for them all

Home Forums Scripting Javascript Tutorials Javascript [SOLVED]: Perform a set of recursive HTTP GET calls, and wait for them all

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #245687

    Cloudy Point
    Keymaster

    QuestionQuestion

    I have a REST service, offering a list of ‘Json’ objects, and each object may potentially have a link for another resource of its own class. Starting with a particular one, I need to fetch them all, performing a recursive http call.
    So I wrote:

    var steps = [];
    var recursiveLookup = function(processId) {
      return $.ajax({
        url: SERVER_URL + processId,
        success: function (activity) {
          // Activity has a set of json objects called steps
          var rtn = activity.path.map(step => {
            if (step.type != "Node") {
              steps.push(step);
            } else {
              return recursiveLookup(step.subProcessIntanceId);
            }
          }).filter(a => a != undefined);
          return rtn;
        }
      });
    }
    

    That would correctly load all objects into the global steps var.
    I need to be sure the method has finished, so I wrote:

    var promises = recursiveLookup(processId);
    Promise.all(promises).then(function () {
       console.log(steps);
    });
    

    But it’s not working, as the ‘recursiveLookup’ is returning the promise of $.ajax, instead of the set of promises pretended to be returned with the success method.

    Furthermore, is it possible to get the steps as a returned value from the ‘recursiveLookup’ method instead, of using it as a global variable?

    #245688

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    Nested recursion is not within my confort zone but maybe this will work:

    var recursiveLookup = function(processId,steps=[]) {
      return $.ajax({
        url: SERVER_URL + processId,
      })
      .then(
        function (activity) {
          // Activity has a set of json objects called steps
          steps = steps.concat(
            activity.path.filter(
              step => step.type !== "Node"
            )
          );
          return Promise.all(
            activity.path.filter(
              step => step.type === "Node"
            )
            .map(
              step=>
                recursiveLookup(step.subProcessIntanceId,steps)
            )
          ).then(
            result=>steps.concat(result)
          )
        }
      );
    }
    

    For tail call optimization to work the last thing the function does should be to call the recursive function but I think in promise chains it doesn’t matter too much.

    Source: https://stackoverflow.com/questions/48022735/perform-a-set-of-recursive-http-get-calls-and-wait-for-them-all
    Author: HMR
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.