I'm having problem calling async function inside a while loop.
the problem is 'while' statement will end before its underlying function result appear and thats because it's async function. the code is like below:
while (end < min) {
db.collection('products').count({
tags: {
$in: ['tech']
}
}, function(err, result) {
if (result) {
a = result;
}
});
max = min;
min = max - step;
myitems.push(a);
}
res.send(myitems);
and at the end i could not send the result because all of while iteration should finish before sending the final result. how could i modify the code to solve such a problem?
thanks in advance
res.send()when they are all done? Or, are you fine sending all the requests at once and you just want to know when they are all done? FYI, in case you hadn't realized it, you can't use awhileloop and expect it to "wait" for async operations to be done. You will have to iterate a different way, but which method depends upon some details of your operation (thus the questions I've asked).db.collection()requests appear to be identical. There appears to be some missing code that is relevant.