Q:

When a nodejs callback will be called an unknown number of times and you need to do something after the last one

As I understand it, the node-netstat package parses the output of the netstat command and it calls the callback I supply, once per line of data it parses. 
I could do with knowing when it's made its last call, so I know to callback the function that was supplied to my function by elsewhere, but I'm not really sure how to do this..
this.myfunc = function(callback){

  netstat(null, function(data){
    //netstat will call this function X times. I'd like to accumulate data
  });

  callback( ..data from netstat.. );

}

If netstat's callback only fired  once, with all the data, then I could probably have called callback at the end of function(data), but the multi-calls is confounding that. What do we do in situations like this? (Note also, it's a really prehistoric version of node: 0.10.24)

A:

You can pass an option object to netstat(options, handler) function.
In option object, there is a done field which you can pass a callback function.
More information about option object can be found here

