I'm trying to write my own wrapper for the mongojs collection.find method that should return the collection items selected by the specified query (querying isn't implemented yet, it should simply select all results). The problem is that I'm not getting back an array of results. It seems like the find method does some kind of async callback. So how can I force a synchronous call or force my script to wait?
Collection.prototype.find = function () {
var result = new Array;
if (Bridge.isServer) {
db.collection(name).find(function(err, items) {
items.forEach(function(item) {
result.push(item);
});
});
}
return result;
}
I think you should consider making your functions asynchronous, but if you insist on writing synchronous functions, there is a github project for making async functions synchronous.
Here's another SO post dealing with the same topic: Convert an asynchronous function to a synchronous function