Discussion:
[elm-discuss] explain how Task works
m***@gmail.com
2017-06-04 12:56:41 UTC
Permalink
Can you explain, at a beginner's level, how Task works in the following 2
lines under Process.sleep ?


delay : Time.Time -> msg -> Cmd msg
delay time msg =
Process.sleep time
|> Task.andThen (always <| Task.succeed msg)
|> Task.perform identity

Thanks
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Peter Damoc
2017-06-09 06:31:41 UTC
Permalink
Process.sleep time is creating a Task that will be running for the duration
of the time and then produce an empty tuple.

Task.andThen is taking this produced task and after it finishes it calls
the provided function (always <| Task.succeed msg)

(always <| Task.succeed msg) is equivalent to (\_ -> Task.succeed msg) and
is a function that ignores its argument and produces a Task that finishes
immediately producing the provided msg.

Task.perform identity takes the above produced Task and converts it to a
Cmd. identity is defined as identity x = x this means that whatever is the
result of the task will be the result of the Cmd.

That being said, the function looks a little bit silly as the Task.andThen
is redundant. It should have been:

delay : Time -> msg -> Cmd msg
delay time msg =
Process.sleep time
|> Task.perform (always msg)


you can see it working here:
https://ellie-app.com/3r6knRYxB2Za1/0
Post by m***@gmail.com
Can you explain, at a beginner's level, how Task works in the following 2
lines under Process.sleep ?
delay : Time.Time -> msg -> Cmd msg
delay time msg =
Process.sleep time
|> Task.andThen (always <| Task.succeed msg)
|> Task.perform identity
Thanks
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
There is NO FATE, we are the creators.
blog: http://damoc.ro/
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...