ingo
ingo

Reputation: 856

nodejs fs.watch async way

I try to figure out what file in a folder is changed while upload files. For this I tried to use fs.watch. I use

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()

But it looks like that nothing inside the //Code part is not executed. I think something I misunderstand here, right? Can anybody give me a hint how to watch a folder async?

Upvotes: 1

Views: 1506

Answers (1)

Sagi Rika
Sagi Rika

Reputation: 2979

You have to put the desired code to run on each file change inside the callback function, like this:

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");
});

Read more about callbacks here.

Upvotes: 1

Related Questions