Im pretty new to node.js, so i'm wondering how to know when all elements are processed in lets say:
["one", "two", "three"].forEach(function(item){
processItem(item, function(result){
console.log(result);
});
});
...now if i want to do something that can only be done when all items are processed, how would i do that?
No one sees the imperfections like the artist does and you must learn to accept them. (If you see no imperfections in your art, look closer – they're there.) With imperfections accepted, you then have to decide if you are happy with the result. If the answer is “yes”, then you are finished.
A 'finished' artwork is a piece that has been worked to a particular level of detail. There are various levels of 'finish' from a basic outline to a 'finished' sketch, then from a basic drawing to a highly 'finished' piece of work.
You can use async module. Simple example: The
async.map(['one','two','three'], processItem, function(err, results){
// results[0] -> processItem('one');
// results[1] -> processItem('two');
// results[2] -> processItem('three');
});
The callback function of async.map will when all items are processed. However, in processItem you should be careful, processItem should be something like this:
processItem(item, callback){
// database call or something:
db.call(myquery, function(){
callback(); // Call when async event is complete!
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With