0
const navTimeout = new Promise((resolve, reject) => {
    setInterval(async () => {
        try {
            await workbook.xlsx.readFile("variables.xlsx")
            const sheet = workbook.getWorksheet(1)
            if(sheet.getCell('B13').value === "D")
                return resolve(true)
        } catch (error) {
            console.log("Workbook being used")
        }
    }, 5000)
})

I have the following piece of code. So my doubt is once the promise gets fulfilled and the resolve is done do I need to call clearTimeout() or it will automatically stop executing? I just don't want it to occupy any pc resources.

XMehdi01
  • 5,538
  • 2
  • 10
  • 34
Vivek Y V
  • 3
  • 2
  • Check this - https://stackoverflow.com/questions/109086/stop-setinterval-call-in-javascript?rq=2 – Adarsh Konchady Jul 05 '23 at 21:11
  • You should stop it. It will not stop automatically. – trincot Jul 05 '23 at 21:12
  • Not your question, but if the file loads, but cell B13 is something different, are you really going to load the same file again and again? – trincot Jul 05 '23 at 21:20
  • You should not be doing any `async` or other promise stuff inside a `new Promise` executor. Create a `delay = t => new Promise(resolve => { setTimeout(resove, t); })` function, then use a normal loop `while (true) { await workbook.xlsx.readFile(…); …; await delay(5000); }` – Bergi Jul 05 '23 at 23:05

1 Answers1

1

It will not automatically stop executing, you have to stop it by clearing it manually:

const navTimeout = new Promise((resolve, reject) => {
  const intervalId = setInterval(async () => {
    try {
      await workbook.xlsx.readFile("variables.xlsx");
      const sheet = workbook.getWorksheet(1);
      if (sheet.getCell('B13').value === "D") {
        clearInterval(intervalId); //stop it here
        resolve(true);
      }
    } catch (error) {
      console.log("Workbook being used");
    }
  }, 5000);
});
XMehdi01
  • 5,538
  • 2
  • 10
  • 34