title: How to not call context.done() twice in Azure Function when using request() and callback?
body: <p>I'm writing an Azure Function that makes a call to a Google API using the npm module request. The callback that processes <code>body</code> needs to call <code>context.done()</code> or else <code>context.res</code> is out of scope (if I set it within the callback and then wait to use it at the end of my script through <code>context.done()</code>).</p>
  
  <p>The <em>problem</em> is I'm not understanding how to both handle errors and return a response to the client <em>and</em> use the callback.</p>
  
  <p>Any and all comments on my code are welcome. I want to improve.</p>
  
  <pre class="lang-js prettyprint-override"><code>var request = require('request');
  
  module.exports = function (context, req) {
  
      if (req.body) {
          incoming = req.body
  
          if (incoming.components &amp;&amp; incoming.components.city
              &amp;&amp; incoming.components.state
              &amp;&amp; incoming.components.country) {
  
              // locality = city
              // administrative_area = state
              var country = incoming.components.country;
              var administrative_area = incoming.components.state;
              var locality = incoming.components.city;
  
              request.get({
                  url: "https://maps.googleapis.com/maps/api/geocode/json?components=country:US|locality:city|administrative_area:CA&amp;key=..."
              }, function (error, response, body) {
                  if (error) {
                      context.log(error);
                  }
  
                  if (!error &amp;&amp; response.statusCode == 200) {
                      context.log(body)
                  }
                  context.res = {
                      status: 200,
                      body: "asdf"
                  };
                  context.done();
              });
  
              context.log("here");
          } else {
              context.res = {
                  status: 400,
                  body: 'There was data but not the right kind. Try {"components": {"country": "US","city": "city","state": "CA"}'
              };
          }
  
      }
      else {
          context.res = {
              status: 400,
              body: 'Nothing was received. Try {"components": {"country": "US","city": "city","state": "CA"}'
          };
      }
      context.done();
  };
  </code></pre>