【发布时间】:2011-10-09 01:01:08
【问题描述】:
我有一个循环,里面有一个异步调用,带有一个回调。为了能够继续前进,我需要一直触发整个循环的回调,然后显示循环的结果。
我尝试过的所有控制方法都不起作用(尝试过 Step、Tame.js、async.js 等) - 关于如何继续前进的任何建议?
array = ['test', 'of', 'file'];
array2 = ['another', 'array'];
for(i in array) {
item = array[i];
document_ids = new Array();
for (i2 in array2) {
item2 = array2[i2];
// look it up
mongodb.find({item_name: item2}).toArray(function(err, documents {
// because of async,
// the code moves on and calls this back later
console.log('got id');
document_ids.push(document_id);
}))
}
// use document_ids
console.log(document_ids); // shows []
console.log('done');
}
// shows:
// []
// done
// got id
// got id
【问题讨论】:
标签: asynchronous node.js