title: How to implement async/await in Dart
body: <p>I think I understand the idea behind <em>async</em>, returning a Future, but I am not clear on how <em>async</em> behaves on a very basic level.  From my understanding, it does not automatically create asynchronous behavior in a program.  For example:</p>
  
  <pre><code>import 'dart:async';
  main() {
    a();
    b();
  }
  a()  {
    new Timer(new Duration(milliseconds: 20), () {});  // create latency
    print("a");
  }
  b()  {
    print("b");
  }
  // a
  // b
  </code></pre>
  
  <p>If you put <em>async</em> after the a(), the b() is executed first asynchronously, and a() executes after the given latency:</p>
  
  <pre><code>import 'dart:async';
  main() {
    a();
    b();
  }
  a() **async** {
    new Timer(new Duration(milliseconds: 20), () {});  // create latency
    print("a");
  }
  b()  {
    print("b");
  }
  // b 
  // a
  </code></pre>
  
  <p>But if you put <em>async</em> after both a() and b(), a() executes first, similar to not using <em>async</em> at all:</p>
  
  <pre><code>import 'dart:async';
  main() {
  a();
  b();
  }
  a() **async** {
    new Timer(new Duration(milliseconds: 20), () {});  // create latency
    print("a");
  }
  b() **async** {
    print("b");
  }
  //a
  //b
  </code></pre>
  
  <p>Is using <em>async</em> on all functions cancelling the <em>async</em> function completely?</p>
  
  <p>Now, I think that the main() <em>async</em> does not actually activate the asynchronous behavior by itself.  If you add <em>async</em> after the main(), nothing changes.  However, it allows for you to use <em>await</em>, in case you must wait for the a() function to finish first, before you continue the program.  Is this right?</p>
  
  <pre><code>import 'dart:async';
  main() **async** {
    **await** a();
    b();
  }
  a() **async** {
  new Timer(new Duration(milliseconds: 20), () {});  // create latency
  print("a");
  }
  b() {
  print("b");
  }
  // waits for the value of a() if you put **await** keyword
  // a
  // b
  </code></pre>
  
  <p>However, I see main() <em>async</em> {} alot as well as using it after the script html tag, yet there is no <em>await</em> anywhere.  Does it mean different things in different contexts?  I hope I have explained the logic adequately.  Can you explain how I am misunderstanding the use of async/await?  Thank you.</p>