Supponendo che si sta pensando di usare Promises
in quanto non è stato fornire una richiamata nella vostra firma del metodo, si potrebbe verificare l'esistenza del file e guardare la directory, allo stesso tempo, quindi risolvere se il file esiste, o il file viene creato prima che si verifichi il timeout.
function checkExistsWithTimeout(filePath, timeout) {
return new Promise(function (resolve, reject) {
var timer = setTimeout(function() {
watcher.close();
reject(new Error('File did not exists and was not created during the timeout.'));
}, timeout);
fs.access(filePath, fs.constants.R_OK, function (err) {
if (!err) {
clearTimeout(timer);
watcher.close();
resolve();
}
});
var dir = path.dirname(filePath);
var basename = path.basename(filePath);
var watcher = fs.watch(dir, function (eventType, filename) {
if (eventType === 'rename' && filename === basename) {
clearTimeout(timer);
watcher.close();
resolve();
}
});
});
}
fonte
2017-12-12 02:19:50
Quale sistema operativo stai prendendo di mira? Su Linux/OSX puoi fare in modo che Node.js guardi una directory per le modifiche. – Brad
Sto eseguendo il codice su Linux – wong2
questo potrebbe essere utile; http://nodejs.org/docs/latest/api/fs.html#fs_fs_watchfile_filename_options_listener – renatoargh