This library helps you run tasks in parallel when you only need the results
if every task finishes successfully, similar to Promise.all() in Javascript. A
good use case is handling the result of multiple HTTP requests.
Attempt a list of tasks which will update when all the tasks have finished
or when one fails. Similar to a Task.sequence except in parallel.
type Msg
= DownloadUpdated (ListMsg Http.Error String)
| DownloadFailed Http.Error
| DownloadCompleted (List String)
fetchNames : ( ListState String, nextCmd )
fetchNames =
attemptList DownloadUpdated [ fetchFirstName, fetchSecondName, fetchThirdName ]Attempt a single task. The benefit of this over Task.attempt is that it handles routing the result to the provided success and failure messages. You can reuse those error messages for different tasks if they're handled the same way. Since there are no other tasks, you don't need to manage any additional updates.
type Msg
= ErrorOcurred Http.Error
| FetchCompleted MyData
doTask : Cmd Msg
doTask =
attempt FetchCompleted ErrorOcurred fetchMyDataYou will have to pass internal messages and commands along in your update function in order to eventually get your results.
Handle updates for two tasks by calling update2 inside of your main update
function to keep this library's internal state updated. If they have either all
finished successfully or one has failed, the corresponding message you provided
will be sent to your main update function. Maintain a copy of the returned
state to pass in on each subsequent update. This step is required with
attempt[n] functions.
type Msg
= DownloadUpdated (Task.Parallel.Msg2 Http.Error Actor Film)
| DownloadFailed Http.Error
| DownloadCompleted Actor Film
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
DownloadUpdated taskMsg ->
let
( nextTaskState, nextCmd ) =
Task.Parallel.update2 model.taskState taskMsg DownloadCompleted DownloadFailed
in
( { model | taskState = nextTaskState }, nextCmd )
DownloadCompleted actor film ->
( { model | actor = actor, film = film, Cmd.none )
DownloadFailed err ->
( { model | loadingError = Just err }, Cmd.none )Call updateList inside of your main update function to check if the
tasks have failed or finished. Maintain a copy of the returned state to pass in
on each subsequent updateList. This step is required with
attemptList.
type Msg
= DownloadUpdated (Task.Parallel.ListMsg Actor Http.Error)
| DownloadFailed Http.Error
| DownloadCompleted (List Actor)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
DownloadUpdated taskMsg ->
let
( nextTaskState, nextCmd ) =
Task.Parallel.updateList model.taskState taskMsg DownloadCompleted DownloadFailed
in
( { model | taskState = nextTaskState }, nextCmd )
DownloadCompleted actors ->
( { model | actorList = actors, Cmd.none )
DownloadFailed err ->
( { model | loadingError = Just err }, Cmd.none )Opaque type for storing state of tasks.
Opaque type for updating state of tasks.
Opaque type for storing state of task lists.
Opaque type for updating state of task lists.
Attempt two tasks which will send an update when either all tasks finish successfully or one fails. The returned
Statewill be used in your main update function to callupdateand pass internal messages.