title: How can I return data from a JS closure in a Node route?
body: <p>I am setting up a NodeJS application and i need to get data from <code>etcd</code>.  I am using the <code>nodejs-etcd</code> module.</p>
  
  <p>The following is how I am attempting to return a value from a closure within a closure:</p>
  
  <pre><code>router.get('/', function(req, res, next) {
  
    var etcd = req.etcd;
  
    var result = etcd.read({key: '/reward/christopher'}, function (err, result, body) {
  
      // parse the json body so that it can be read
      object = JSON.parse(body);
  
      // return the value of the key
      return object.node.value;
    });
  
    console.log(result);
  
  });
  </code></pre>
  
  <p>However all I ever get is the client connection data from the <code>etcd</code> object and not the value that I am trying to return:</p>
  
  <pre><code>Client {
    version: 'v2',
    baseurl: 'http://turtle-host-03.turtlesystems.local:4001/v2',
    agent: false }
  GET / 500 807.246 ms - 1338
  GET /stylesheets/style.css 304 6.856 ms - -
  </code></pre>
  
  <p>The value from the <code>etcd</code> server in the callback function is correct, it is just getting it into the outer function.</p>
  
  <p>I am suspect this is a misunderstanding on my part as to how this closure works in the callback for the read method in <code>etcd</code>.</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>Added code as requested instead of linking to a gist.</p>