Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when finished

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?

like image 516
BadKnees Avatar asked May 27 '12 20:05

BadKnees


People also ask

How do you know when art is finished?

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.

What is a finished drawing?

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.


1 Answers

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!
   });
}
like image 60
Mustafa Avatar answered Oct 13 '22 11:10

Mustafa