Run tasks in parallel and handle the results only if every task finishes
successfully, similar to Promise.all() in Javascript.
See the examples folder for full code examples.
It is common to have several tasks where the results only matter together. For example, you may need to complete multiple HTTP requests before your page can be rendered. In order to avoid running the tasks in sequence which is slower, you typically have to
This library is designed to do that for you.
Instead of using Task.attempt, use one of the helper functions to run up to 5
tasks of different result types (or a list of the same type). It will return a
tuple with some internal state and a command.
( internalState, fetchCmd ) =
attempt5
RequestsUpdated
Api.fetchUser
Api.fetchOptions
Api.fetchLocations
Api.fetchChat
Time.nowStore the state and pass along the command. Your model will need to store a
State type matching the number of your tasks.
State5 User Options Locations Chat Time.PosixThe message you passed in to the helper function will need to accept an internal
Msg with a similar type annotation, but including the error type to handle.
type Msg
= RequestsUpdated (Msg5 Http.Error User Options Locations Chat Time.Posix)
| RequestsFinished User Options
| RequestFailed Http.Errorand finally your update function will only need to handle three cases
case msg of
RequestsUpdated internalMsg ->
let
( nextState, nextCmd ) =
update5 model.internalState internalMsg RequestsFinished RequestFailed
in
( { model | internalState = nextState }, nextCmd )
RequestsFinished user options locations chat time ->
( { model | user = user, options = , locations = locations, chat = chat, currentTime = time }, Cmd.none )
RequestFailed err ->
( { model | maybeError = Just err, Cmd.none )Maybe.map). For HTTP requests, this is a limit I haven't run into yet. For
lists of tasks, there is no limit.