我试图使用async.each
对数组进行同步迭代
async.each(supplier_array, function(supplier) {
console.log('looking at : ' + supplier);
knex(host_name + '.order_item').where({
supplier: supplier,
order_id: order_id
}).then(function(data) {
console.log(data);
knex(host_name + '.distributor').select()
.then(function(data) {
console.log(data);
}).catch(function(error) {
console.log('error: ' + error);
});
}).catch(function(error) {
console.log('error: ' + error);
});
});
我的supplier_array
有三个元素。因此,应用程序应该(同步地):
对于供应商1/第一arr /第一阵列元素:
对于供应商2/第二个数组元素:
对于供应商3/第三个阵列元件:
但是,它是异步的:
有人能帮我实现同步运行在async
内部的步骤的预期效果吗?
提前感谢!
发布于 2016-03-23 02:32:28
如果要按串行顺序迭代它们,则应该使用async.eachSeries
。试着做这样的事情:
async.eachSeries(supplier_array, function(supplier, callback) {
console.log('looking at : ' + supplier);
knex(host_name + '.order_item').where({
supplier: supplier,
order_id: order_id
}).then(function(data) {
console.log(data);
knex(host_name + '.distributor').select()
.then(function(data) {
console.log(data);
callback(); // Advance to next iteration here
}).catch(function(error) {
console.log('error: ' + error);
callback(error); // Also callback when error occurs
});
}).catch(function(error) {
console.log('error: ' + error);
callback(error); // Also callback when error occurs
});
});
https://stackoverflow.com/questions/36168666
复制相似问题