# Correct way to wait for a Timer callback to execute

**URL:** <https://discourse.julialang.org/t/correct-way-to-wait-for-a-timer-callback-to-execute/42320>\
**Category:** General Usage\
**Created:** [June 30, 2020, 7:17pm UTC](https://discourse.julialang.org/t/correct-way-to-wait-for-a-timer-callback-to-execute/42320 "2020-06-30T19:17:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [June 30, 2020, 7:17pm UTC](https://discourse.julialang.org/t/correct-way-to-wait-for-a-timer-callback-to-execute/42320/1 "2020-06-30T19:17:06Z")

</div>

Use case in case it matters is to have non-flaky unit tests without having to over-abstract things (which is why I’d prefer to not have some boolean iscallbackdone inside the callback).

```julia
function testtimer()
    ss = ""
    tt = Timer(1) do t 
        ss *= "aa"
    end
    wait(tt)
    ss *= "bb"
    sleep(1.1)
    return ss
end

julia> testtimer() # Want "aabb"
"bbaa"

```

Putting a `yield()` or similar between timer start and `wait(tt)` makes the function produce the desired output, but I doubt it will be robust enough to pass on free CI.

This works, but I want to have a reference to the timer so I can stop (close) it:

```julia

function testtask()
    ss = ""
    tt = @async begin
            wait(Timer(1) do t 
                ss *= "aa"
            end) 
        end
    wait(tt)
    ss *= "bb"
    sleep(1.1)
    return ss
end

julia> testtask()
"aabb"

```

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [July 2, 2020, 6:07pm UTC](https://discourse.julialang.org/t/correct-way-to-wait-for-a-timer-callback-to-execute/42320/2 "2020-07-02T18:07:15Z")

</div>

Not sure what your ultimate goal is here. But I think you could do something like:

```julia
function testtimer()
    cond=Condition()
    c = 0
    ss = ""
    tt = Timer(1) do t 
        ss *= "aa"
        c += 1
        notify(cond)
    end
    while c < 2
        wait(cond)
        if c == 1
            ss *= "bb"
        end
    end
    return ss
end

```

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [July 10, 2020, 7:36pm UTC](https://discourse.julialang.org/t/correct-way-to-wait-for-a-timer-callback-to-execute/42320/3 "2020-07-10T19:36:20Z")

</div>

Thanks, although that looks a bit more messy than what I had hoped for.

I ended up doing something like this instead:

```julia

function starttimer(delay, stuff...)
return Timer(delay) do _
     timerexpiry(stuff...)
end
function timerexpiry(stuff...)
  #stuff with stuff
end

```

And then just test `timerexpiry` in testcases.
