Get updates via , Github, or RSS | About

Episode 37: RemoteData Jun 8, 2017

Rather than track HTTP requests’ statuses with something like an isLoading: Bool property, use the RemoteData type and pattern. This provides a succinct way to represent the different states a request can be in, without muddying up your model.

Examples

Main.elm

type RemoteData e a
    = NotAsked
    | Loading
    | Failure e
    | Success a


type alias Model =
    { wins : RemoteData Http.Error Int
    , games : RemoteData Http.Error Int
    }

View.elm

case model.wins of
    NotAsked ->
        text ""

    Loading ->
        div [ class "throbber" ] []

    Failure e ->
        div [ class "error" ] [ text "error" ]

    Success wins ->
        h4 [] [ text <| toString wins ]