0

I have a for-loop statement and an async MongoDB inside loop body. What I want to do is to make a find query from my MongoDB database, and push the result into an Array.

Here is the code:

function() arrResult() {
  var arr = [];
  for(...) {
    collection.find({ foo: i }, function (err, cursor) {
      arr.push(cursor);   
    }    
  }
  return arr;
}

But it's obvious that the return value of the function would be an empty Array.

I want to tackle this problem using Q module. Is there any solutions?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201

2 Answers2

2

I want to tackle this problem using Q module. Is there any solutions?

Yes, promises are a very easy abstraction to deal with this. You can execute the queries in parallel, and collect their results with all.

In particular, with Q it would look like this:

function arrResult(…) {
    var promises = [];
    for (…)
        promises.push( Q.ninvoke(collection, "find", {foo: i}) );
    return Q.all(promises);
}

arrResult(…).then(function(arr) {
    …
}, function(err) {
    // first error, if any occured
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Great. Could you please complete it using `Q.defer()`? – Afshin Mehrabani Feb 25 '14 at 18:59
  • @AfshinMehrabani: Why would I need to? One [never uses `Q.defer`](https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#wiki-the-deferred-anti-pattern) manually. `Q.ninvoke` and the other [callback-function method](https://github.com/kriskowal/q/wiki/API-Reference#wiki-promise-for-function-methods) perfectly avoid this. – Bergi Feb 25 '14 at 19:03
  • Just for educational purposes. – Afshin Mehrabani Feb 25 '14 at 19:12
  • 1
    I think that's pretty well covered in [*Using deferreds* in the Q docs](https://github.com/kriskowal/q/#using-deferreds). You can translate the example with `FS.readFile` directly into one for `collection.find` – Bergi Feb 25 '14 at 19:17
1

You need a sync mechanism that acts like a process gate. Each returning query has to arrive at the gate, e.g. decrements some counter and deposit its result. When all arrived at the gate, a final callback does return the collected results.

mvw
  • 5,075
  • 1
  • 28
  • 34