title: How to await and return the result of a http.request(), so that multiple requests run serially?
body: <p>Assume there is a function <code>doRequest(options)</code>, which is supposed to perform an HTTP request and uses <code>http.request()</code> for that.</p>
  
  <p>If <code>doRequest()</code> is called in a loop, I want that the next request is made after the previous finished (serial execution, one after another). In order to not mess with callbacks and Promises, I want to use the async/await pattern (transpiled with Babel.js to run with Node 6+).</p>
  
  <p>However, it is unclear to me, how to wait for the response object for further processing and how to return it as result of <code>doRequest()</code>:</p>
  
  <pre><code>var doRequest = async function (options) {
  
      var req = await http.request(options);
  
      // do we need "await" here somehow?
      req.on('response', res =&gt; {
          console.log('response received');
          return res.statusCode;
      });
      req.end(); // make the request, returns just a boolean
  
      // return some result here?!
  };
  </code></pre>
  
  <p>If I run my current code using <em>mocha</em> using various options for the HTTP requests, all of the requests are made simultaneously it seems. They all fail, probably because <code>doRequest()</code> does not actually return anything:</p>
  
  <pre><code>describe('Requests', function() {
      var t = [ /* request options */ ];
      t.forEach(function(options) {
          it('should return 200: ' + options.path, () =&gt; {
              chai.assert.equal(doRequest(options), 200);
          });
      });
  });
  </code></pre>