我正在尝试使用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
有3个元素。因此,应该发生的是应用程序应该(同步):
对于供应商1 /第一区域/第一数组元素:
对于供应商2 /第二个数组元素:
对于供应商3 /第三个数组元素:
但是,它是异步运行的:
console.log(供应商)
console.log(order_item)
console.log(order_item)
console.log(发行人)
有人可以帮助我达到async
同步执行内部步骤的预期效果吗?
提前致谢!
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
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句