How to? Timeout exception for function

Is there a way to run a function that, if it takes longer than time t to run, it times out?

Looking for something along the lines of

try {

func();

} catch (e:Timeout) {
// timed out running try block
}

I simply can’t find any way to do this, and would really like to know if anyone’s been able to?

Start the timer inside func()?
And then return or throw the Timeout.
I assume of course you can alter func…

I want to be able to do this with any given function, without altering the function itself.

Hmm. Then I guess you’ll have to start a new process, then you can just kill it any time you like…

Could you elaborate?

I don’t believe there’s a way to kill a running process at any time.

http://api.haxe.org/sys/io/Process.html

So, do a var p = new Process(...) and then p.kill() it.

I’ve seen that - but that’s specifically an external process, no? You can’t call a haxe function directly using new Process can you?

Nope. That’s an entirely new process.
Maybe there are other ways, but I don’t know of any…

I think my question boils down to, is there a way to interrupt/kill a running function in haxe from an external function?

I think it would only work if you had a function that could work as multiple iterations (and you stop iterating when some condition changes), another option may be if it works in a different thread, though I do not how stable it would be if you are allowed to kill the thread

You can use deferred/promises ( https://github.com/jdonaldson/promhx )

With your function call also start a haxe.timer.delay - if the timer is reached and the promise wasn’t resolved yet, reject the promise

( that doesn’t kill the running function though, but you have essentially a timeout handler. if the function you called exposes cancel methods, call them within the timeout function as well )