# Timed loop

**URL:** <https://discourse.julialang.org/t/timed-loop/48802>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 22, 2020, 6:38am UTC](https://discourse.julialang.org/t/timed-loop/48802 "2020-10-22T06:38:41Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [October 22, 2020, 7:26am UTC](https://discourse.julialang.org/t/timed-loop/48802/4 "2020-10-22T07:26:07Z")

</div>

We have [`timedwait`](https://docs.julialang.org/en/v1/base/parallel/#Base.timedwait) in Base (but this polls) or you could follow the following pattern:

```julia
cond = Condition()
Timer(x->notify(cond), max_time)
t = @async begin
    # do stuff
    notify(cond)
end
wait(cond)
t.state == :done ? fetch(t) : :timeout

```

see also: [How to wait on a condition for tasks](https://discourse.julialang.org/t/how-to-wait-on-a-condition-for-tasks/48023)

Better yet to put it in a function! Then you can return a value or a timeout.

---

_[View the full topic](https://discourse.julialang.org/t/timed-loop/48802)._
