Thanks for this useful snippet.
I wanted something very similar but with the possibility of a default value in case of failure. So I came up with this:
macro timeout(seconds, expr, fail)
quote
tsk = @task $expr
schedule(tsk)
Timer($seconds) do timer
istaskdone(tsk) || Base.throwto(tsk, InterruptException())
end
try
fetch(tsk)
catch _
$fail
end
end
end
x = @timeout 1 begin
sleep(1.1)
println("done")
1
end "failed"