等待文件写入完成

时间:2019-04-01 12:37:19

标签: node.js fs

我正在使用fs.watch()监视共享文件夹。有时,文件会添加到目录中,并且外部程序会在其中写入文本5-10秒钟。写入文件后,我需要对文件进行处理。

chokidar库具有一个awaitWriteFinish属性,这是我试图实现的。出于性能原因,我不能使用chokidar,并且我不了解那里的实现。

我的只监听事件的代码:

let fsWait = false //used to prevent multiple events fired at once. 
fs.watch(`\\\\192.168.1.103\\Test\\`, (eventType, filename) => {
    if (filename) {
        if (fsWait)
            return
        fsWait = setTimeout(() => {
            fsWait = false
        }, 100)
        console.log(eventType)
        console.log(filename)
    }
})

我该怎么办?

0 个答案:

没有答案