title: How to sequentially execute async functions in dart with WebSocket
body: <p>I am current writing a WebSocket client. There are a few functions that I need to happen sequentially.</p>
  <p>currently I do it in this way</p>
  <ol>
  <li>Connect to server</li>
  <li>Then set up a listener.</li>
  <li>Add Conditional statement to listener.(To check if response id's match request ids)</li>
  <li>send request One with id</li>
  <li>if response id matches request One Id then process request</li>
  <li>send request Two</li>
  <li>repeat</li>
  </ol>
  <p>This makes sequential actions look something like this</p>
  <pre><code>_channel.stream.listen((response) {
     if(response.id == requestOne.id) {
        handleRequestOneResponse(response);
     }
  
     if (response.id == requestTwo.id){
        handleRequestTwoResponse(response);
     }
     ...
  });
  
  sendActionOneRequest();
  
  handleRequestOneResponse() {
     // Some processing
     sendActionTwoRequest();
  }
  
  handleRequestTwoResponse() {
     // some processing
  
     sendActionThreeRequest();
  }
  
  </code></pre>
  <p>What I want to do is</p>
  <p>Set up an async function
  Send the request to the WebSocket server.
  Pause the execution of the async function.
  wait until a matching response comes from the server
  complete the async function.</p>
  <p>This would allow me to write a series of actions like</p>
  <pre><code>await actionOne();
  await actionTwo();
  await actionThree();
  </code></pre>
  <p>I'm thinking I can set up and destroy a stream listener in each <code>action</code> function but I don't know how to wait for a specific response before exiting.</p>
  <p>On the other hand I think I can even use the existing listener on the outside, but I still can't figure out to wait till a specific response comes in before moving forward.</p>
  <p>As it is I have to jump through every function to find out what comes after the other and there are more than 5 requests that have to be sent sequentially.</p>