We can use Promise.race to wait for the first arrived result on the thenable
chain. The Task module doesn't seem to support it yet, Task.sequence is only the equivalent to Promise.all.
Non-thenable Solution demo:
import Process
import Task
init () =
( Nothing, Cmd.batch [ after 2 "2nd", after 1 "1st" ] )
after seconds name =
Process.sleep (1000 * seconds)
|> Task.map (always name)
|> Task.perform Done
type Msg
= Done String
update (Done name) model =
case model of
Nothing ->
( Debug.log name <| Just name, Cmd.none )
_ ->
( Debug.log name model, Cmd.none )
main =
Platform.worker
{ init = init
, update = update
, subscriptions = always Sub.none
}
Run it, output as expected:
1st: Just "1st"
2nd: Just "1st"