1

I want to stop a function execution from caller after 10s, e.g:

let done = false

setTimeout(() => {
  if (!done) throw new Error('Function timeout')
}, 10000)

await someClientCodeHere()

done = true

This code works if something is waiting (i.e: on task queue/priority queue) but doesn't work if I put a for loop inside someClientCodeHere.

My question is: Is there anyway to stop a function that is on callStack?

Natan Deitch
  • 556
  • 8
  • 12
  • 1
    You can kill the process - from the outside. You cannot stop the currently running function from the next turn of the event loop. – Bergi Jun 05 '20 at 17:49
  • 1
    Does this answer your question? [How to implement a "function timeout" in Javascript - not just the 'setTimeout'](https://stackoverflow.com/questions/8778718/how-to-implement-a-function-timeout-in-javascript-not-just-the-settimeout) – Elan Hamburger Jun 05 '20 at 17:49

0 Answers0