Cancel other promise execution on Promise.race reject

38 Views Asked by At

I am developing an asynchronous Node.js app that implements Promise.race for a portion of its logic:

async function callDeobfuscator(ast) {
  const withTimeout = (promise) => {
    let timeoutPid;
    const timeoutPromise = new Promise((resolve, reject) => 
      timeoutPid = setTimeout(reject, timeout));
    return Promise.race([
        promise,
        timeoutPromise
    ]).finally(() => {
        if (timeoutPid) {
          clearTimeout(timeoutPid);
        }
    });
  };
  return withTimeout(processSource(ast));
};

The issue is, processSource(ast) may run indefinitely/forever and I want to be able to cancel its execution once it times out. Is there a way to do this?

0

There are 0 best solutions below