title: How to make a function wait until a callback has been called using node.js
body: <p>I have a simplified function that looks like this:</p>
  
  <pre><code>function(query) {
    myApi.exec('SomeCommand', function(response) {
      return response;
    });
  }
  </code></pre>
  
  <p>Basically i want it to call <code>myApi.exec</code>, and return the response that is given in the callback lambda. However, the above code doesn't work and simply returns immediately.</p>
  
  <p>Just for a very hackish attempt, i tried the below which didn't work, but at least you get the idea what i'm trying to achieve:</p>
  
  <pre><code>function(query) {
    var r;
    myApi.exec('SomeCommand', function(response) {
      r = response;
    });
    while (!r) {}
    return r;
  }
  </code></pre>
  
  <p>Basically, what's a good 'node.js/event driven' way of going about this? I want my function to wait until the callback gets called, then return the value that was passed to it.</p>