0

I have a long running function call and I want to limit its execution time.

function f(a,b,c) {
  // some long-running code
  return value;
}

var val = f(a,b,c); // this call should be limited to x milliseconds
                    // if the function doesn't return we continue with a fall-back value (which is available in the calling method)

How can I call this code with a timeout, so that if the execution finishes it will return value and otherwise it will return a default value?

I found this question but it's 9 years old and I couldn't get the most recent answer (the one using Promises) modified for my use-case. Is there a simpler way to achieve this?

martin
  • 2,150
  • 1
  • 34
  • 48
  • 1
    The worker approach is the only one that will work. Use the code in that answer, and if the timeout is reached without a message from the worker, use the fallback value – CertainPerformance Jun 20 '21 at 04:34
  • This is possible to solve with Promises. I have an answer, please re-open so I can post it. – martin Jun 26 '21 at 02:13

0 Answers0