Tasks make it easy to describe asynchronous operations that may fail, like HTTP requests or writing to a database. For more information, see the Elm documentation on Tasks.
A task that succeeds immediately when run.
succeed 42 -- results in 42A task that fails immediately when run.
fail "file not found" : Task String aTransform a task.
map sqrt (succeed 9) == succeed 3Put the results of two tasks together. If either task fails, the whole thing fails. It also runs in order so the first task will be completely finished before the second task starts.
map2 (+) (succeed 9) (succeed 3) == succeed 12Put the results of two tasks together. If either task fails, the whole thing fails. It also runs in order so the first task will be completely finished before the second task starts.
This function makes it possible to chain tons of tasks together and pipe them all into a single function.
(f `map` task1 `andMap` task2 `andMap` task3) == map3 f task1 task2 task3Chain together a task and a callback. The first task will run, and if it is successful, you give the result to the callback resulting in another task. This task then gets run.
succeed 2 `andThen` (\n -> succeed (n + 2)) == succeed 4This is useful for chaining tasks together. Maybe you need to get a user from your servers and then lookup their picture once you know their name.
Start with a list of tasks, and turn them into a single task that returns a list. The tasks will be run in order one-by-one and if any task fails the whole sequence fails.
sequence [ succeed 1, succeed 2 ] == succeed [ 1, 2 ]This can be useful if you need to make a bunch of HTTP requests one-by-one.
Recover from a failure in a task. If the given task fails, we use the callback to recover.
fail "file not found" `onError` (\msg -> succeed 42) -- succeed 42
succeed 9 `onError` (\msg -> succeed 42) -- succeed 9Transform the error value. This can be useful if you need a bunch of error types to match up.
type Error = Http Http.Error | WebGL WebGL.Error
getResources : Task Error Resource
getResources =
sequence [ mapError Http serverTask, mapError WebGL textureTask ]Translate a task that can fail into a task that can never fail, by
converting any failure into Nothing and any success into Just something.
toMaybe (fail "file not found") == succeed Nothing
toMaybe (succeed 42) == succeed (Just 42)This means you can handle the error with the Maybe module instead.
If you are chaining together a bunch of tasks, it may be useful to treat a maybe value like a task.
fromMaybe "file not found" Nothing == fail "file not found"
fromMaybe "file not found" (Just 42) == succeed 42Translate a task that can fail into a task that can never fail, by
converting any failure into Err something and any success into Ok something.
toResult (fail "file not found") == succeed (Err "file not found")
toResult (succeed 42) == succeed (Ok 42)This means you can handle the error with the Result module instead.
If you are chaining together a bunch of tasks, it may be useful to treat a result like a task.
fromResult (Err "file not found") == fail "file not found"
fromResult (Ok 42) == succeed 42Run a task on a separate thread. This lets you start working with basic
concurrency. In the following example, task1 and task2 will be interleaved.
If task1 makes a long HTTP request, we can hop over to task2 and do some
work there.
spawn task1 `andThen` \_ -> task2Make a thread sleep for a certain amount of time. The following example sleeps for 1 second and then succeeds with 42.
sleep 1000 `andThen` \_ -> succeed 42Abstract type that uniquely identifies a thread.
Represents asynchronous effects that may fail. It is useful for stuff like HTTP.
For example, maybe we have a task with the type (
Task String User). This means that when we perform the task, it will either fail with aStringmessage or succeed with aUser. So this could represent a task that is asking a server for a certain user.