Stream: beginners

Topic: ✔ Task.await syntax sugar confusion


view this post on Zulip Sylvain Brunerie (Oct 26 2024 at 08:19):

Hi! I’m trying to build a CLI script with Roc, struggling a little bit but getting somewhere! The main difficulty is around tasks I guess. Currently my issue is the following. I have this functioning code at the end of my main loop (passed to Task.loop):

    when (oldState, newState) is
        (DirtySince _, Clean) ->
            Stdout.line! "no more changes!"
            Sleep.millis! pollDelayInMillis
            Task.ok (Step newState)

        _ ->
            Stdout.line! (stateToStr newState)
            Sleep.millis! pollDelayInMillis
            Task.ok (Step newState)

How can I move the last two lines of each branch (Sleep.millis and Task.ok) in a single place?

view this post on Zulip Anton (Oct 26 2024 at 08:28):

I would add a function like below and call it from both branches;

sleepOk = \sleepMillis, newState ->
    Sleep.millis! sleepMillis
    Task.ok (Step newState)

Does that work for you?

view this post on Zulip Sylvain Brunerie (Oct 26 2024 at 08:33):

Oh ok makes sense! That works, I thought there would be a more clever function but that’s ok.

view this post on Zulip Sylvain Brunerie (Oct 26 2024 at 08:33):

Thanks!

view this post on Zulip Notification Bot (Oct 26 2024 at 22:25):

Sylvain Brunerie has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC