title: Sleep() not working as expected
body: <p>In my code, I'm trying to put a certain delay before continuing to the rest of the code. Pretty basic. I'm using a custom sleep function because javascript's native sleep function is not working at all. I'm actually working in app script in google spreadsheets so maybe that's why. But the following code is in the <code>&lt;script&gt;</code> tag of the html file in spreadsheet app script.</p>
  
  <p>Anyway, when I use <code>sleep()</code>, it is being executed before the <code>setTimeout</code></p>
  
  <pre><code>function get_ids(db){
  
      window.alert("before window.ids is saved?");
      google.script.run.withSuccessHandler(getIdsFromAppscript).getIdsFromModel(db); 
      //this returns a result to getIdsFromAppscript but the following code doesn't wait 
      //for the result to be returned so I want to use sleep before the ids 
      //variable is returned by get_ids
  
  
      setTimeout(function(){
          window.alert("checking if ids is saved after 10s?");
          window.alert("In timeout ids="+window.ids);
          var ids= window.ids; //is non empty
      },10000);
  
      sleep(10000);
  
      var ids= window.ids;
      window.alert("after wait");
      window.alert("after sleep ids="+ids); //is empty but should be non empty
  
      return ids; //=window.ids , Need to return a non-empty result
  
  }
  
  function getIdsFromAppscript(result){
  window.ids=result;
  
  }
  </code></pre>
  
  <p>and the sleep function:</p>
  
  <pre><code>function sleep(ms) {
    var start = new Date().getTime(), expire = start + ms;
    while (new Date().getTime() &lt; expire) { }
    return;
  }
  </code></pre>
  
  <p><strong>Current Order of output</strong> based on <code>window.alert()</code>:</p>
  
  <pre><code>1) before window is saved?
  2) after sleep
  3) after sleep ids=  //basically empty which shouldn't be the case
  4) checking if ids is saved after 10s?
  5) In timeout ids= [.....] //non empty list
  </code></pre>
  
  <p>However, my <strong>desired output order</strong> is:</p>
  
  <pre><code>1) before window is saved?
  2) checking if ids is saved after 10s?
  3) In timeout ids= [.....] //non empty list
  4) after sleep
  5) after sleep ids= [...] //should be a non empty list 
  </code></pre>
  
  <p>The reason why I'm writing setTimeout is to show that after 10 seconds, the result is being stored in <code>window.ids</code> however even after I give a sleep for 10 seconds, or even 30 seconds, I can't get the result from window.ids. </p>
  
  <p>What exactly am I doing wrong here? Thanks in advance~</p>