G
As an option in colbeca:app.get('/some', function(req, res) {
var data = []; // Массив с тем, что будем выводить
var firstLoop = function(i, some1, callback1){
if(i < some1){
...
var secondLoop = function(s, some2, callback2){
if(s < some2){
Model.count({ ... }, function(err, count){
data.push(d);
secondLoop(s++, some2, callback2);
});
}else{
callback2();
}
}
secondLoop(0, some2, function(){
firstLoop(i++, some1, callback1);
});
}else{
callback1();
}
}
firstLoop(0, some1, function(){
res.json({ message: 'Some ok 2', content: data });
});
});
We replace the cycles with the recurring functions that cause ourselves as long as the condition is correct and callback when the cycles end. The second cycles are waiting for the request to be completed, the data in the set and the next iteration, as soon as it is finished, starts the next irration of the first cycle, as soon as the first cycle is completed, sends the answer. Thus, all requests and cycles will be carried out first and then answered.They're doing the same. https://learn.javascript.ru/promise or libraries such as https://caolan.github.io/async/ ♪