title: Why couldn't popular JavaScript runtimes handle synchronous-looking asynchronous script?
body: <p>As <a href="https://gist.github.com/domenic/3889970">cowboy says down in the comments here</a>, we all want to "write [non-blocking JavaScript] asynchronous code in a style similar to this:</p>
  
  <pre><code> try 
   {
      var foo = getSomething();   // async call that would normally block
      var bar = doSomething(foo);  
      console.log(bar); 
   } 
   catch (error) 
   {
      console.error(error);
   }
  </code></pre>
  
  <p>"</p>
  
  <p>So people have come up solutions to this problem like </p>
  
  <ul>
  <li>callback libraries (eg <a href="https://github.com/caolan/async">async</a>) </li>
  <li><a href="http://www.html5rocks.com/en/tutorials/es6/promises/">promises</a> </li>
  <li><a href="http://www.wekeroad.com/2012/04/05/cleaning-up-deep-callback-nesting-with-nodes-eventemitter/">event patterns</a></li>
  <li><a href="http://blog.rivaliq.com/develop-double-time-node-plus-streamline/">streamline</a></li>
  <li><a href="http://nodejs.org/api/domain.html">domains</a> and </li>
  <li><a href="http://devsmash.com/blog/whats-the-big-deal-with-generators">generators</a>.</li>
  </ul>
  
  <p>But none of these lead to code as simple and easy to understand as the sync-style code above. </p>
  
  <p><strike>So why isn't possible for javascript compilers/interpreters to just NOT block on the statements we currently know as "blocking"? </strike> So why isn't possible for javascript compilers/interpreters to handle the sync syntax above AS IF we'd written it in an async style?"</p>
  
  <p>For example, upon processing <code>getSomething()</code> above, the compiler/interpreter could just say "this statement is a call to [file system/network resource/...], so I'll make a note to listen to responses from that call and in the meantime get on with whatever's in my event loop". When the call returns, execution can proceed to <code>doSomething()</code>.</p>
  
  <p>You would still maintain all of the basic features of popular JavaScript runtime environments</p>
  
  <ul>
  <li>single threaded</li>
  <li>event loop </li>
  <li>blocking operations (I/O, network, wait timers) handled "asynchronously"</li>
  </ul>
  
  <p>This would be simply a tweak to the syntax, that would allow the interpreter to pause execution on any given bit of code whenever IT DETECTS an async operation, and instead of needing callbacks, code just continues from the line after the async call when the call returns.</p>
  
  <p>As <a href="http://devsmash.com/blog/whats-the-big-deal-with-generators">Jeremy</a> says </p>
  
  <blockquote>
    <p>there is nothing in the JavaScript runtime that will preemptively
    pause the execution of a given task, permit some other code to execute
    for a while, and then resume the original task</p>
  </blockquote>
  
  <p>Why not? (As in, "why couldn't there be?"... I'm not interested in a history lesson)</p>
  
  <p>Why does a developer have to care about whether a statement is blocking or not? Computers are for automating stuff that humans are bad at (eg writing non-blocking code).  </p>
  
  <p>You could perhaps implement it with </p>
  
  <ul>
  <li>a statement like <strike><code>"use noblock"</code>;</strike> (a bit like <code>"use strict";</code>) to turn this "mode" on for a whole page of code. EDIT: <code>"use noblock"</code>; was a bad choice, and misled some answerers that I was trying to change the nature of common JavaScript runtimes altogether. Something like <code>'use syncsyntax';</code> might better describe it.</li>
  <li>some kind of <code>parallel(fn, fn, ...);</code> statement allowing you to run things in parallel while in <code>"use syncsyntax"</code>; mode - eg to allow multiple async activities to be kicked off at once</li>
  <li>EDIT: a simple sync-style syntax <code>wait()</code>, which would be used instead of <code>setTimeout()</code> in <code>"use syncsyntax"</code>; mode</li>
  </ul>
  
  <p>EDIT: </p>
  
  <p>As an example, instead of writing (standard callback version)</p>
  
  <pre><code>function fnInsertDB(myString, fnNextTask) {
    fnDAL('insert into tbl (field) values (' + myString + ');', function(recordID) {
      fnNextTask(recordID);
    });
  }
  
  fnInsertDB('stuff', fnDeleteDB);
  </code></pre>
  
  <p>You could write</p>
  
  <pre><code>'use syncsyntax';
  
  function fnInsertDB(myString) {
    return fnDAL('insert into tbl (field) values (' + myString ');');  // returns recordID
  }
  
  var recordID = fnInsertDB('stuff'); 
  fnDeleteDB(recordID);
  </code></pre>
  
  <p>The <code>syncsyntax</code> version would process exactly the same way as the standard version, but it's much easier to understand what the programmer intended (as long as you understand that <code>syncsyntax</code> pauses execution on this code as discussed).</p>