title: javascript nested loops waiting for user input
body: <p>I built a C interpreter in C# a while ago and have now begun converting it to Javascript. Everything was going fine until I realized js has no sleep function. My interpreter uses a recursive parser and it pauses for user input while it is nested several functions deep (in C# I used waithandle in a second thread). I have looked at setInterval and setTimeout but they are asynchronous /non-blocking; of course a busywait is out of the question and I looked at a timed_queue  implementation I found on SO but no luck. I have tried the parser both in the main window and in a webworker. I am using jQuery. I have limited experience with js and am looking for ideas to pursue. I know little about continuation passing style, or yield and am wondering if they might hold the key. Here is a bit cut from the code to show some of the controlscript. Any ideas please...</p>
  
  <pre><code>var STATE = {
      START: "START",
      RUN: "RUN", //take continuous steps at waitTime delay
      STEP: "STEP", //take 1 step
      PAUSE: "PAUSE",//wait for next step command
      STOP: "STOP",
      ERROR: "ERROR"
  }
  var state = state.STOP;
  
  function parsing_process() //long process we may want to pause or wait in 
  {
      while(token !== end_of_file)//
      {
          //do lots of stuff - much of it recursive
          //the call to getNextToken will be encountered a lot in the recursion
          getNextToken();
          if (state === STATE.STOP)
              break;
      }
  }
  
  function getNextToken()
  {
      //retrieve next token from lexer array
      if (token === end_of_line)
      {
          //tell the gui to highlight the current line
          if (state === STATE.STOP) 
              return;
          if (state === STATE.STEP)//wait for next step
          {
              //mimick wait for user input by using annoying alert
              alert("click me to continue")
          }
  
          if (state === STATE.RUN) {
              //a delay here - set by a slider in the window
              //a busy wait haults processing of the window
          }
      }
  }
  </code></pre>
  
  <p>I have gotten this to work in Firefox using task.js</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;task.js examples: sleep&lt;/title&gt;
      &lt;script type="application/javascript" src="task.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      Only works in FIREFOX
      &lt;button onclick="step()"&gt;Step&lt;/button&gt;
      &lt;button onclick="run()"&gt;Run&lt;/button&gt;
      &lt;button onclick="stop()"&gt;Stop&lt;/button&gt;
      &lt;pre style="border: solid 1px black; width: 300px; height: 200px;" id="out"&gt;
  &lt;/pre&gt;
  
      &lt;script type="application/javascript;version=1.8"&gt;
  
          function start() {
              process();
          }
  
          function step() {
              if (state === STATE.STOP)
                  start();
              state = STATE.STEP;
          }
          function run() {
              if (state === STATE.STOP)
                  start();
              state = STATE.RUN;
          }
          function stop() {
              state = STATE.STOP;
          }
  
          var STATE = {
              START: "START",
              RUN: "RUN", //take continuous steps at sleepTime delay
              STEP: "STEP", //take 1 step
              PAUSE: "PAUSE",//wait for next step command
              STOP: "STOP",
              ERROR: "ERROR"
          }
  
          var state = STATE.STOP;
          var sleepTime = 500;
  
          function process() {
              var { spawn, choose, sleep } = task;
              var out = document.getElementById("out");
              var i=0;
              out.innerHTML = "i="+i;
              var sp = spawn(function() {
                  while(state !== STATE.STOP)
                  {
                      i++;
                      out.innerHTML = "i="+i;
                      if (state === STATE.RUN)
                      {
                          yield sleep(sleepTime);
                      }
                      if (state === STATE.STEP)
                          state = STATE.PAUSE;
                      while (state===STATE.PAUSE)
                      {
                          yield;
                      }
                  }
              });
          }
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I would appreciate it if someone who knew something about promises could give me some more clues. My application is not a consumer one but it would be nice if it ran in more than Firefox</p>