Promise.race
Maybe
要跟踪我们是否收到响应:
type Thing =
...
getThings : String -> Task Never (List Thing)
getThings url =
...
type alias Model =
{ things : Maybe (List Thing) }
type Msg
= GotThings (List Thing)
init =
( { things = Nothing }
, Cmd.batch
[ Task.perform GotThings (getThings "https://a-server.com/things")
, Task.perform GotThings (getThings "https://a-different-server.com/things")
]
)
update msg model =
case msg of
GotThings things ->
case model.things of
Nothing ->
( { things = Just things }, Cmd.none )
Just _ ->
-- if we have already received the things, ignore any subsequent requests
( model, Cmd.none )
view model =
...