title: Wait for request to finish
body: <p>I am currently trying to make a node.js wrapper for a MediaWiki extension and I need to be able to log in. Everything so far seems to be working but I cannot get the script to halt and wait for the request to finish before returning. This is crucial because the request must finish before it returns in order for it to return the proper value and not undefined. I am using the request module for node.js. The current HTTP POST handler is as follows</p>
  
  <pre><code>var post = function(data) {
  
      var r;
      var options = {};
          options.url = module.exports.apipath;
          options.method = "POST";
          options.form = data;
  
      request(options, function(error, response, body) {
  
           if (!error) {
  
              if(response.statusCode == 200) {
  
                  try {
  
                      if((typeof body) == "string") {
  
                          var result = JSON.parse(body);
  
                          r = result;
                      } else {
  
                          r = body;
                      }
                  } catch (e) {
  
                      return;
                  }
              }
          } else {
  
              console.log("Error: " + error);
          }
      });
  
      return r;
  };
  </code></pre>