title: Stopping and later continuing a recursive tree traversal
body: <p>I'm using a <s>breadth-first</s> depth-first (?) search to map out files and folders. I'm also recording the mapped data, and building a JSON structure that shows the folders and their files/details.</p>
  
  <p>I want to halt the execution at an arbitrary point, saving the JSON data, and then restarting the execution, and continuing from where I left off. Here is my code (This is Google Apps Script, essentially glorified cloud-executed JavaScript).</p>
  
  <pre><code>function MapDrive() {      
    var structure = {};
    try {    
      var parentFolder = DriveApp.getRootFolder();
  
      getChildFolders(parentFolder, structure, {path: parentFolder.getName()});
      var blob = CreateJSONBlob(structure);
      MailApp.sendEmail('myEmail', 'Folder Tree', 'Drive Map Attached' ,{ attachments: [blob]})
    } catch (e) {
      Logger.log(e.toString());   
    }
  }
  
  //Recursive breadth-first folder and file mapping
  function TraverseDriveTree(parent, structure, path){
    if(CheckExecutionTime()){
      var childFolders = parent.getFolders();
      var parentName = parent.getName(); 
  
      //If there is no parent path add it
      if(typeof path[parentName] === 'undefined'){
        path[parentName] = {path: path.path};
      }
  
      //If parent doe snot exist in structure, then add it and it's files
      if(typeof structure[parentName] === 'undefined'){
        structure[parentName] = {};
        structure[parentName]['files'] = GetFilesInfo(parent.getFiles(), path[parentName].path);
      }  
  
      while (childFolders.hasNext()) {    
        var childFolder = childFolders.next();
        var childfolderName = childFolder.getName();  
        path[parentName][childfolderName] = {path: path[parentName].path + " &gt; " +childfolderName};
  
        if(typeof structure[parentName][childfolderName] === 'undefined'){
          structure[parentName][childfolderName] = {};
        }
  
        structure[parentName][childfolderName]["files"] = GetFilesInfo(childFolder.getFiles(), path[parentName][childfolderName].path);
  
        // Recursive call for any sub-folders
        TraverseDriveTree(childFolder, structure[parentName][childfolderName], path[parentName]);    
      }     
  
    } else {
      SaveCurrentState(structure, parent, path)
    }
  }
  </code></pre>
  
  <p>I do not need help with saving the JSON, or anything like that. I need help figuring out how to continue a recursive tree traversal after stopping it, and continuing to add items to the data structure from where I left off.</p>