I am trying to synchronously iterate over an array using 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);
});
});
My supplier_array has 3 elements. So what SHOULD happen is the app should (synchronously):
For supplier 1 / first arr / first array element:
- console.log(supplier)
- console.log(order_item)
- console.log(distributor)
For supplier 2 / second array element:
- console.log(supplier)
- console.log(order_item)
- console.log(distributor)
For supplier 3 / third array element:
- console.log(supplier)
- console.log(order_item)
- console.log(distributor)
HOWEVER, it acts asynchronously:
- console.log(supplier)
- console.log(supplier)
console.log(supplier)
console.log(order_item)
- console.log(order_item)
console.log(order_item)
console.log(distributor)
- console.log(distributor)
- console.log(distributor)
Can someone help me achieve the desired effect of running through the steps inside of async synchronously?
Thanks in advance!
-
You might have to do a "return" on each knex promise. That acknowledges the async to let it know that there's a promise to process. Try it, and let me know if it's the right answer. I ran into this issue with knex myself. Also, you can move the inside "then" on line 9 out a level, since you're returning a promise.Handonam– Handonam2016年03月23日 02:30:00 +00:00Commented Mar 23, 2016 at 2:30
1 Answer 1
You should use async.eachSeries if you want to iterate them in serial order. Try something like this:
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
});
});
2 Comments
callback() will tell it to look at the next supplier, and apply all those processes to the next supplier?Explore related questions
See similar questions with these tags.