Created
March 15, 2017 16:42
-
-
Save ravicious/f7a8fa12591e37443c44ace76fd5ed13 to your computer and use it in GitHub Desktop.
Reimplementation of Elm's RemoteData http://package.elm-lang.org/packages/krisajenkins/remotedata/latest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
// https://flowtype.org/try/#0PTAEAEDMBsHsHcBQiCmAPADrATgF1LgJ4YqgBKKAtrLigCICGuDAPAKLbY4AqxKANKABqDaAFcUvEgD5QAXkShQAH1ABvUAGdmtAFygA5ADsaAQU0BrFABMDoAL6KV6rTpT6DcBtYCWRgOZ2jkqqGtpM7oaQDD7i2CgGgiicOPocXNhSpMHOYW4emmIAxkUompqJoABuohL6IuKSfA4A3MjoWHigkGJGRbg+sEagJrjmVtbsKZl8gg0SWdIAFACU+hTUtIzMUxlZc7VNMupO8bhi2MN5ER6j4zZBbY6omDj4PX0DQ6BevgG7PFmwkOi1W6yoNHoTFY6UBJAOjUWJyUZwuV1cN0Mvz8gVaiGeHTe3V6-UGw2isQuKABM3hwMRfGWyQyaWmWTW5AhW2hNP29IWjORoFRlxc4T0URicQSSWmeIJry6H1J30KJTKml5QPmRxQyxqjXqIL4HI2kO2MLZ2uNxzUpxQ51F1wlBjVpXKlQNEnlyGgDu6sFg4M2UJ22mwOME4ZxsjkWmK7s0SwMDHg8FAhBQDAAFgYVm1IIH5N0pVTk7BsyNA3mC0W49iAqta7Bi3dLDYm0A | |
export type RemoteData<ErrorType, ValueType> = | |
| { state: 'notAsked' } | |
| { state: 'loading' } | |
| { state: 'failure', error: ErrorType } | |
| { state: 'success', value: ValueType }; | |
export function notAsked<ErrorType, ValueType>(): RemoteData<ErrorType, ValueType> { | |
return { state: 'notAsked' }; | |
} | |
export function loading<ErrorType, ValueType>(): RemoteData<ErrorType, ValueType> { | |
return { state: 'loading' }; | |
} | |
export function failure<ErrorType, ValueType>(error: ErrorType): RemoteData<ErrorType, ValueType> { | |
return { state: 'failure', error }; | |
} | |
export function success<ErrorType, ValueType>(value: ValueType): RemoteData<ErrorType, ValueType> { | |
return { state: 'success', value }; | |
} | |
let foo: RemoteData<string, string> = success('aww yeah'); | |
foo = failure('oh noo'); | |
foo = loading(); | |
foo = notAsked(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment