Task
Core
Work that runs outside update. attempt reports back. spawn does not.
Types
Task
type Task x a = TaskA computation that can succeed with a or fail with x.
Examples
succeed 42 : Task x Int
fail "boom" : Task String aValues
succeed
succeed : a -> Task x aA task that succeeds immediately.
Examples
succeed 42
|> andThen (\n -> succeed (n + 1))
-- Task that succeeds with 43fail
fail : x -> Task x aA task that fails immediately.
Examples
fail "not found"
|> onError (\_ -> succeed 0)
-- Task that succeeds with 0map
map : (a -> b) -> Task x a -> Task x bTransform a task's success value.
Examples
map String.length (succeed "hi")
-- Task that succeeds with 2map2
map2 : (a -> b -> result) -> Task x a -> Task x b -> Task x resultCombine two tasks.
Examples
map2 (+) (succeed 2) (succeed 3)
-- Task that succeeds with 5map3
map3 : (a -> b -> c -> result) -> Task x a -> Task x b -> Task x c -> Task x resultCombine three tasks.
Examples
map3 (\a b c -> a + b + c) (succeed 1) (succeed 2) (succeed 3)
-- Task that succeeds with 6map4
map4 : (a -> b -> c -> d -> result) -> Task x a -> Task x b -> Task x c -> Task x d -> Task x resultCombine four tasks.
Examples
map4 (\a b c d -> a + b + c + d)
(succeed 1) (succeed 2) (succeed 3) (succeed 4)
-- Task that succeeds with 10map5
map5 : (a -> b -> c -> d -> e -> result) -> Task x a -> Task x b -> Task x c -> Task x d -> Task x e -> Task x resultCombine five tasks.
Examples
map5 (\a b c d e -> a + b + c + d + e)
(succeed 1) (succeed 2) (succeed 3) (succeed 4) (succeed 5)
-- Task that succeeds with 15andThen
andThen : (a -> Task x b) -> Task x a -> Task x bChain a task onto a success value.
Examples
succeed 2
|> andThen (\n -> succeed (n * 3))
-- Task that succeeds with 6onError
onError : (x -> Task y a) -> Task x a -> Task y aRecover from a failure with another task.
Examples
fail "file not found"
|> onError (\_ -> succeed 42)
-- Task that succeeds with 42mapError
mapError : (x -> y) -> Task x a -> Task y aTransform a task's failure.
Examples
fail "oops"
|> mapError String.toUpper
-- Task that fails with "OOPS"sequence
sequence : List (Task x a) -> Task x (List a)Run tasks in order and collect the successes.
Examples
sequence [ succeed 1, succeed 2 ] == succeed [1, 2]perform
perform : (a -> msg) -> Task Never a -> Cmd msgRun a task that cannot fail and send the success into update.
Examples
perform GotValue (succeed 42)
-- Cmd Msgattempt
attempt : (Result x a -> msg) -> Task x a -> Cmd msgRun a task and send the Result into update.
Examples
attempt GotResult (fail "boom")
-- Cmd Msg delivering Err "boom"from
from : (() -> Result x a) -> Task x aWrap an effect the server already performs. The function runs in a task process.
Examples
from (\_ -> Ok 42)
-- Task x Int that succeeds with 42spawn
spawn : Task x a -> ()Run a task on the node and ignore its result. Failure is logged.
Examples
spawn (succeed ())
-- ()