summaryrefslogtreecommitdiffstats
path: root/src/RemoteData.elm
blob: 717385125ea9ab5fa3bc03eeaf2714d1c25cce61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module RemoteData exposing (..)

import Http


type RemoteData a
    = Initial
    | Loading
    | Failure Http.Error
    | Success a


withDefault : a -> RemoteData a -> a
withDefault default rd =
    case rd of
        Success value ->
            value

        _ ->
            default


map : (a -> b) -> RemoteData a -> RemoteData b
map fn rd =
    case rd of
        Initial ->
            Initial

        Loading ->
            Loading

        Failure err ->
            Failure err

        Success value ->
            Success (fn value)


andThen : (a -> RemoteData b) -> RemoteData a -> RemoteData b
andThen callback rd =
    case rd of
        Initial ->
            Initial

        Loading ->
            Loading

        Failure err ->
            Failure err

        Success value ->
            callback value