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"
Promise.raceas a self-contained function requires maintaining local state to track whether or not it's been resolved already, which as you probably know isn't possible in Elm.But you can accomplish the same thing relatively easily by tracking the state in the model yourself. Here's an example using
Maybeto track whether we've received a response: