title: Google Apps Script withSuccessHandler is running before function fully executes
body: <p>I have two files in an Apps Script project.  One is a .gs file acting as a "server" and one is a .html file containing JavaScript as per Google's Apps Script guidelines.</p>
  
  <p>Everything has been going swimmingly for the first 40 hours of development on this project.  I have the following line of code outside of any function, in between two   tags in the .html file:</p>
  
  <pre><code>google.script.run.withSuccessHandler(setSheetData).getSheetData();
  </code></pre>
  
  <p>Documentation: <a href="https://developers.google.com/apps-script/guides/html/reference/run#withSuccessHandler(Function)" rel="nofollow noreferrer">https://developers.google.com/apps-script/guides/html/reference/run#withSuccessHandler(Function)</a></p>
  
  <p>According to the documentation, getSheetData() should first execute in the .gs file, and return a value that is then passed into setSheetData which exists in the .html file.</p>
  
  <p>Server file:</p>
  
  <pre><code>  function getSheetData() {
    var ss = SpreadsheetApp.getActive();
    var activeSheet = ss.getActiveSheet();
    var sheetName = activeSheet.getName();
    var sheetVals = activeSheet.getDataRange().getValues();
    return [sheetName, sheetVals];
  }
  </code></pre>
  
  <p>Html file:</p>
  
  <pre><code>function setSheetData(data) {
      alert(data);
      sheetName = data[0];
      sheetData = data[1];
      headers = sheetData[0];
      document.getElementById('sheetLook').innerHTML = 'Looking at sheet: ' + sheetName;
  }
  </code></pre>
  
  <p><strong>How I know it is a matter of execution speed:</strong>
  Currently the alert() call just prints out null.  The sheet it is drawing from contains 4 rows of data.  However, all other things remaining the same, if I simply am looking at a sheet with 0-1 rows of data, it correctly alerts the entire data vals.</p>
  
  <p>Inside of getSheetData() if I add Logger.log(sheetVals) it correctly logs the entire sheet's data regardless of size.  The issue is that the successhandler is executing <strong>before</strong> it has time to evaluate.</p>