I have a loop with an asynchronous call inside it with a callback. To be able to move on, I need the callback to work for the entire loop to the end in order to display the results of the loop.
Every way I tried to control this does not work (tried Step, Tame.js, async.js and others) - any suggestions on how to move forward?
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
Filo stacks
source share