nodejs fs.watch异步方式

问题描述 投票:0回答:1

我试图找出在上传文件时文件夹中的哪个文件已更改。为此,我尝试使用fs.watch。我用

const watcher = fs.watch(watchDir, (eventname, filename) => {
  console.log("Watcher: " + filename);
});

//Code
const getCookie = ClientFunction(() => {
    return document.cookie;
});
let xmlresult = await helpers.getXMLInfo('', testCorpusid, caseid);
console.log("Test message");
//Code

watcher.close()

但是看起来// Code部分内部什么都没有执行。我觉得我在这里误会了吧?有人可以提示我如何观看文件夹异步吗?

javascript node.js fs
1个回答
0
投票

您必须将所需的代码放在回调函数中的每个文件更改上运行,如下所示:

const watcher = fs.watch(watchDir, async (eventname, filename) => {
  console.log("Watcher: " + filename);

  const getCookie = ClientFunction(() => {
      return document.cookie;
  });
  let xmlresult = await helpers.getXMLInfo('', testCorpusid, caseid);
  console.log("Test message");
});

阅读有关回调here的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.