function checkFileExists(filePath, timeout=15000) {
  return new Promise((resolve, reject) => {
    const dir = path.dirname(filePath);
    const basename = path.basename(filePath);

    const watcher = fs.watch(dir, (eventType, filename) => {
      if (eventType === 'rename' && filename === basename) {
        clearTimeout(timer);
        watcher.close();
        resolve();
      }
    });

    const timer = setTimeout(() => {
      watcher.close();
      reject(new Error(' [checkFileExists] File does not exist, and was not created during the timeout delay.'));
    }, timeout);

    fs.access(filePath, fs.constants.R_OK, err =>  {
      if (!err) {
        clearTimeout(timer);
        watcher.close();
        resolve();
      }
    });
  });
}
/**
 * From @xprudhomme.
 * Check if file exists, watching containing directory meanwhile.
 * Resolve if the file exists, or if the file is created before the timeout
 * occurs.
 * @param {string} filePath
 * @param {integer} timeout
 * @returns {!Promise<undefined>} Resolves when file has been created. Rejects
 *     if timout is reached.
 */
From @xprudhomme.
Check if file exists, watching containing directory meanwhile.
Resolve if the file exists, or if the file is created before the timeout
occurs.
@param {string} filePath
@param {integer} timeout
@returns {!Promise} Resolves when file has been created. Rejects
if timout is reached.
From @xprudhomme . Check if file exists watching containing directory meanwhile . Resolve if the file exists or if the file is created before the timeout occurs . @param { string } filePath @param { integer } timeout @returns { !Promise } Resolves when file has been created . Rejects if timout is reached .
function checkFileExists(filePath, timeout=15000) {
  return new Promise((resolve, reject) => {
    const dir = path.dirname(filePath);
    const basename = path.basename(filePath);
    const watcher = fs.watch(dir, (eventType, filename) => {
      if (eventType === 'rename' && filename === basename) {
        clearTimeout(timer);
        watcher.close();
        resolve();
      }
    });
    const timer = setTimeout(() => {
      watcher.close();
      reject(new Error(' [checkFileExists] File does not exist, and was not created during the timeout delay.'));
    }, timeout);
    fs.access(filePath, fs.constants.R_OK, err =>  {
      if (!err) {
        clearTimeout(timer);
        watcher.close();
        resolve();
      }
    });
  });
}
function checkFileExists ( filePath , timeout = 15000 ) { return new Promise ( ( resolve , reject ) => { const dir = path . dirname ( filePath ) ; const basename = path . basename ( filePath ) ; const watcher = fs . watch ( dir , ( eventType , filename ) => { if ( eventType === 'rename' && filename === basename ) { clearTimeout ( timer ) ; watcher . close ( ) ; resolve ( ) ; } } ) ; const timer = setTimeout ( ( ) => { watcher . close ( ) ; reject ( new Error ( ' [checkFileExists] File does not exist, and was not created during the timeout delay.' ) ) ; } , timeout ) ; fs . access ( filePath , fs . constants . R_OK , err => { if ( ! err ) { clearTimeout ( timer ) ; watcher . close ( ) ; resolve ( ) ; } } ) ; } ) ; }
From @xprudhomme.
From @xprudhomme .