title: Callback after end of asynchronous recursive function
body: <p>The function below prints Chrome bookmarks in a folder recursively. How could I alter the below function to call another function after the final recursive loop is processed? <code>chrome.bookmarks.getChildren()</code> is asynchronous which makes it difficult to know when the function is done processing everything.</p>
  
  <p>Thanks.</p>
  
  <pre><code>    for (var i = 0; i &lt; foldersArray.length; i++) {
          // The loop makes several calls with different folder IDs.
          printBookmarks(foldersArray[i]);  
      }
  
      // I'd like any code here to be run only after the above has 
      //finished processing    
  
      function printBookmarks(id) {
          chrome.bookmarks.getChildren(id, function(children) {
             children.forEach(function(bookmark) { 
                 console.debug(bookmark.title);
                 printBookmarks(bookmark.id);
             });
          });
      }
  </code></pre>
  
  <p>EDIT: Sorry, I don't think I was clear in the initial code example. I've updated the code to show the problem I'm having with the asynchronous function by calling the function multiple times. I'd like any code after the <code>printBookmarks</code> function calls to wait for all the <code>printBookmarks</code> functions to finish processing.</p>