title: How to make an asynchronous Dart call synchronous?
body: <p>I'm on the way to evaluate Dart for a German company by porting various Java programs to Dart and compare and analyze the results. In the browser Dart wins hands down. For server software performance seemed to be a serious isssue (see <a href="https://stackoverflow.com/questions/28026648/how-to-improve-dart-performance-of-data-conversion-to-from-binary">this question of me</a>) but that got mostly defused.</p>
  
  <p>Now I'm in the area of porting some "simple" command-line tools where I did not expect any serious problems at all but there is at least one. Some of the tools do make HTTP requests to collect some data and the stand-alone Dart virtual machine only supports them in an asynchronous fashion. Looking through all I could find it does not seem to be possible to use any asynchronous call in a mostly synchronous software.</p>
  
  <p>I understand that I could restructure the available synchronous software into an asynchronous one. But this would transform a well-designed piece of software into something less readable and more difficult to debug and maintain. For some software pieces this just does not make sense.
  My question: Is there an (overlooked by me) way to embed an asynchronous call into a synchronously called method?</p>
  
  <p>I imagine that it would not be to difficult to provide a system call, usable only from within the main thread, which just transfers the execution to the whole list of queued asynchronous function calls (without having to end the main thread first) and as soon as the last one got executed returns and continues the main thread.</p>
  
  <p>Something which might look like this:</p>
  
  
  
  <pre class="lang-dart prettyprint-override"><code>var synchFunction() {
    var result;
    asyncFunction().then(() { result = ...; });
  
    resync(); // the system call to move to and wait out all async execution
  
    return result;
  }
  </code></pre>
  
  <p>Having such a method would simplify the lib APIs as well. Most "sync" calls could be removed because the re-synchronisation call would do the job. It seems to be such a logical idea that I still think it somehow exists and I have missed it. Or is there a serious reason why that would not work?
  <hr>
  After thinking about the received answer from <code>lm</code> (see below) for two days I still do not understand why the encapsulation of an asynchronous Dart call into a synchronous one should not be possible. It is done in the "normal" synchronous programing world all the time. Usually you can wait for a resynchronization by either getting a "Done" from the asynchronous routine or if something fails continue after a timeout.</p>
  
  <p>With that in mind my first proposal could be enhanced like that:
  </p>
  
  <pre class="lang-dart prettyprint-override"><code>var synchFunction() {
    var result;
    asyncFunction()
      .then(() { result = ...; })
      .whenComplete(() { continueResync() }); // the "Done" message
  
    resync(timeout); // waiting with a timeout as maximum limit
  
    // Either we arrive here with the [result] filled in or a with a [TimeoutException].
    return result;
  }
  </code></pre>
  
  <p>The <code>resync()</code> does the same that would normally happen after ending the <code>main</code> method of an isolate, it starts executing the queued asynchronous functions (or waits for events to make them executable). As soon as it encounters a <code>continueResync()</code> call a flag is set which stops this asynchronous execution and <code>resync()</code> returns to the main thread. If no <code>continueResync()</code> call is encountered during the given <code>timeout</code> period it too aborts the asynchronous execution and leaves <code>resync()</code> with a <code>TimeoutException</code>.</p>
  
  <p>For some groups of software which benefit from straight synchronous programing (not the client software and not the server software) such a feature would solve lots of problems for the programer who has to deal with asynchrounous-only libraries.</p>
  
  <p>I believe that I have also found a solution for the main argument in <code>lm</code>'s argumentation below. Therefore my question still stands with respect to this "enhanced" solution which I proposed: <strong>Is there anything which really makes it impossible to implement that in Dart?</strong></p>