Loop/block until something becomes true

Help my memory - I’m sure I saw a nice macro somewhere that added an until construct to Elixir,

What i want to do is pretty basic :

# until function returns true, ignore exceptions, block for maximum of 60 seconds.
until(Foo.check_is_something_good, 60_000)  

Just like this linux command :

- until curl \
--silent \
-XGET \
--fail http://127.0.0.1:${EX_RABBIT_POOL_PORT} &> /dev/null \
; do printf '.'; sleep 1; done

I know I could code this in a short time but I keep encountering this same need - wonder if there’s anything pretty out there.

Thanks.

B

You basically just get this for free with tasks:

task = Task.async(fn -> Foo.check_is_good() end)
Task.yield(task, 60_000)
4 Likes

What am I missing?

I don’t see that working unless Foo.check_is_good() is actually Foo.return_when_good() - i.e. yield/2 in the docs doesn’t seem to imply any looping behaviour - so the looping (e.g. recursing) behaviour would have to be built into the function (task) being called.

Here I simply wrote a wait/3 function for the desired effect.

1 Like

Found something like what I’d seen before - a macro to add a while loop to Elixir - I want to block until the condition becomes true (and yes, I’ll need to consider all the timeouts that could happen when I (rpc) call that function) / pages 80-85 metaprogramming elixir.

1 Like