Nodejs check file exists, if not, wait till it exist

I'm generating files automatically, and I have another script which will check if a given file is already generated, so how could I implement such a function:

function checkExistsWithTimeout(path, timeout)

which will check if a path exists, if not, wait for it, util timeout.

thanks~

Check out the Node file system functions http://nodejs.org/api/fs.html I use the following code in a program of mine:

//Get file stats (including size) for file saved to server
fs.stat(__dirname + '/img/' + filename, function(err, stats) {

  if(err) 
    throw err;  

  //if a file
  if (stats.isFile()) {
    //console.log("It\'s a file & stats.size= " + JSON.stringify(stats));    
    console.log("File size saved to server: " + stats.size);    
  };
});