title: How to wait for the result of a mongoose query?
body: <p>I try to filter an array according to the result of a mongoose query. The standard filter function expect the callback to return true or false. My trouble is that this information depends on the asynchronous result of a mongoose findOne query</p>
  
  <pre><code># code that does not work
  myArray.filter = (elem) -&gt;
    MyCollection.findOne {_id : elem}, (err,elem) -&gt;
      result = err==null
    #Need to wait here for the result to be set
    result
  </code></pre>
  
  <p>Anyone has a clue how to resolve that kind problem ?</p>
  
  <p>I tried as well to use the async filter function but I don't think it works in my case (or I maybe I don't understand well)</p>
  
  <p>Here is my understanding of async filter and why (I think) it can't solve my problem :</p>
  
  <pre><code>// code that doesn't work
  async.filter(myArray, function(elem){
    var result = true;
    MyCollection.findOne({_id : elem}, function(err,elem) {
      result = err==null;
    });
    // the filter exits without waiting the update done asychronously in the findOne callback
    return result;
  }, 
  function(results){
    // results now equals an array of the existing files
  });
  </code></pre>