How to know the nodejs process is finished?

Hi there is some nodejs code as follow:

console.log("Start", new Date().getTime()); 
var fs = require('fs');
for (var i = 1; i < 10; i++) {
    fs.readFile("file1.zip",function(err, data) {
      if(err)
        console.log("read error: ", err);
      else {
            fs.writeFile(__dirname + "/file2.zip", data , function(err) {
                if(err) {
                    console.log("write error: ", err);
                }           
            }); 
      }
    });
};
console.log("Finished", new Date().getTime());

I want to get the start and finish time when all the work is done, but it seems like the the second log is too early

Instead of console.log("Finished", new Date().getTime()); you might want to try:

process.on('exit', function () {
  console.log("Finished", new Date().getTime());
});

The second log function is called too early because of node's async behavior. You have to put it inside the last callback:

console.log("Start", new Date().getTime());
var fs = require('fs');
for (var i = 1; i < 10; i++) {
  fs.readFile("file1.zip",function(err, data) {
    if(err) {
      console.log("read error: ", err);
    } else {
      fs.writeFile(__dirname + "/file2.zip", data , function(err) {
        if(err) {
          console.log("write error: ", err);
        }
        // Put it here
        console.log("Finished", new Date().getTime());
      }); 
    }
  });
};