title: Asynchronous Calls and Recursion with Node.js
body: <p>I'm looking to execute a callback upon the full completion of a recursive function that can go on for an undetermined amount of time.  I'm struggling with async issues and was hoping to get some help here.  The code, using the <code>request</code> module, is as follows:</p>
  
  <pre><code>var start = function(callback) {
    request.get({
      url: 'aaa.com'
    }, function (error, response, body) {
      var startingPlace = JSON.parse(body).id;
      recurse(startingPlace, callback);
    });
  };
  
  var recurse = function(startingPlace, callback) {
      request.get({
          url: 'bbb'
      }, function(error, response, body) {
          // store body somewhere outside these funtions
          // make second request
          request.get({
              url: 'ccc'
          }, function(error, response, body) {
              var anArray = JSON.parse(body).stuff;
              if (anArray) {
                  anArray.forEach(function(thing) {
                      request.get({
                          url: 'ddd'
                      }, function(error, response, body) {
                          var nextPlace = JSON.parse(body).place;
                          recurse(nextPlace);
                      });
                  })
              }
          });
      });
      callback();
  };
  
  start(function() {
      // calls final function to print out results from storage that gets updated each     recursive call
      finalFunction();
  });
  </code></pre>
  
  <p>It seems that once my code goes past the <code>for</code> loop in the nested requests, it continues out of the request and ends the initial function call while the recursive calls are still going on.  I want it to not finish the highest-level iteration until all the nested recursive calls have completed (which I have no way of knowing how many there are).</p>
  
  <p>Any help is GREATLY appreciated!</p>