Task

Core

Work that runs outside update. attempt reports back. spawn does not.

Types

Task

type Task x a = Task

A computation that can succeed with a or fail with x.

Examples

succeed 42 : Task x Int
fail "boom" : Task String a

Values

succeed

succeed : a -> Task x a

A task that succeeds immediately.

Examples

succeed 42
    |> andThen (\n -> succeed (n + 1))
-- Task that succeeds with 43

fail

fail : x -> Task x a

A task that fails immediately.

Examples

fail "not found"
    |> onError (\_ -> succeed 0)
-- Task that succeeds with 0

map

map : (a -> b) -> Task x a -> Task x b

Transform a task's success value.

Examples

map String.length (succeed "hi")
-- Task that succeeds with 2

map2

map2 : (a -> b -> result) -> Task x a -> Task x b -> Task x result

Combine two tasks.

Examples

map2 (+) (succeed 2) (succeed 3)
-- Task that succeeds with 5

map3

map3 : (a -> b -> c -> result) -> Task x a -> Task x b -> Task x c -> Task x result

Combine three tasks.

Examples

map3 (\a b c -> a + b + c) (succeed 1) (succeed 2) (succeed 3)
-- Task that succeeds with 6

map4

map4 : (a -> b -> c -> d -> result) -> Task x a -> Task x b -> Task x c -> Task x d -> Task x result

Combine four tasks.

Examples

map4 (\a b c d -> a + b + c + d)
    (succeed 1) (succeed 2) (succeed 3) (succeed 4)
-- Task that succeeds with 10

map5

map5 : (a -> b -> c -> d -> e -> result) -> Task x a -> Task x b -> Task x c -> Task x d -> Task x e -> Task x result

Combine 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 15

andThen

andThen : (a -> Task x b) -> Task x a -> Task x b

Chain a task onto a success value.

Examples

succeed 2
    |> andThen (\n -> succeed (n * 3))
-- Task that succeeds with 6

onError

onError : (x -> Task y a) -> Task x a -> Task y a

Recover from a failure with another task.

Examples

fail "file not found"
    |> onError (\_ -> succeed 42)
-- Task that succeeds with 42

mapError

mapError : (x -> y) -> Task x a -> Task y a

Transform 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 msg

Run a task that cannot fail and send the success into update.

Examples

perform GotValue (succeed 42)
-- Cmd Msg

attempt

attempt : (Result x a -> msg) -> Task x a -> Cmd msg

Run 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 a

Wrap an effect the server already performs. The function runs in a task process.

Examples

from (\_ -> Ok 42)
-- Task x Int that succeeds with 42

spawn

spawn : Task x a -> ()

Run a task on the node and ignore its result. Failure is logged.

Examples

spawn (succeed ())
-- ()