remote_data
A type that represents a remote data fetch
Types
pub type RemoteData(a, error) {
NotAsked
Loading
Failure(error)
Success(a)
}
Constructors
-
NotAsked
-
Loading
-
Failure(error)
-
Success(a)
Functions
pub fn from_list(
data_list: List(RemoteData(a, b)),
) -> RemoteData(List(a), b)
Convert a list of RemoteData to a RemoteData of a list
Examples
from_list([Success(1), Success(2)])
// -> Success([1, 2])
from_list([Success(1), Failure("error")])
// -> Failure("error")
pub fn from_option(
option: Option(a),
or error: b,
) -> RemoteData(a, b)
Convert an Option to a RemoteData
Examples
from_option(Some(1), or: "error")
// -> Success(1)
from_option(None, or: "error")
// -> Failure("error")
pub fn from_result(result: Result(a, b)) -> RemoteData(a, b)
Convert a Result to a RemoteData
Examples
from_result(Ok(1))
// -> Success(1)
from_result(Error("error"))
// -> Failure("error")
pub fn is_failure(data: RemoteData(a, b)) -> Bool
Check if a RemoteData is a Success
Examples
is_failure(Success(1))
// -> False
is_failure(Failure("error"))
// -> True
pub fn is_loading(data: RemoteData(a, b)) -> Bool
Check if a RemoteData is a Success
Examples
is_loading(Success(1))
// -> False
is_loading(Loading)
// -> True
pub fn is_not_asked(data: RemoteData(a, b)) -> Bool
Check if a RemoteData is a Success
Examples
is_not_asked(Success(1))
// -> False
is_not_asked(NotAsked)
// -> True
pub fn is_success(data: RemoteData(a, b)) -> Bool
Check if a RemoteData is a Success
Examples
is_success(Success(1))
// -> True
is_success(Failure("error"))
// -> False
pub fn map(
over data: RemoteData(a, b),
with mapper: fn(a) -> c,
) -> RemoteData(c, b)
Map a function over the success value of a RemoteData
Examples
map(over: Success(1), with: int.to_string)
// -> Success("1")
map(over: Failure("error"), with: int.to_string)
// -> Failure("error")
pub fn map2(
over data1: RemoteData(a, b),
over2 data2: RemoteData(c, b),
with mapper: fn(a, c) -> d,
) -> RemoteData(d, b)
Map a function over the success value of two RemoteData values
Examples
map_2(over: Success(1), over2: Success(2), with: fn(a, b) { a + b })
// -> Success(3)
map_2(over: Failure("error"), over2: Success(2), with: fn(a, b) { a + b })
// -> Failure("error")
pub fn map3(
over data1: RemoteData(a, b),
over2 data2: RemoteData(c, b),
over3 data3: RemoteData(d, b),
with mapper: fn(a, c, d) -> e,
) -> RemoteData(e, b)
The same as map2
, but with three RemoteData values
Check map2
for more details
pub fn map_error(
over data: RemoteData(a, b),
with mapper: fn(b) -> c,
) -> RemoteData(a, c)
Map a function over the error value of a RemoteData
Examples
map_error(over: Success(42), with: fn(_) { "error" })
// -> Success(42)
map_error(over: Failure(42), with: fn(_) { "error" })
// -> Failure("error")
pub fn to_option(data: RemoteData(a, b)) -> Option(a)
Convert a RemoteData to an Option
Examples
to_option(Success(1))
// -> Some(1)
to_option(Failure("error"))
// -> None
pub fn to_result(
data: RemoteData(a, b),
or error: b,
) -> Result(a, b)
Convert a RemoteData to a Result If the data is NotAsked or Loading, it will be converted to an Error with the provided error
Examples
to_result(Success(1), or: "error")
// -> Ok(1)
to_result(Failure("error"), or: "another error")
// -> Error("error")
to_result(Loading, or: "another error")
// -> Error("another error")
pub fn try(
over data: RemoteData(a, b),
with mapper: fn(a) -> RemoteData(c, b),
) -> RemoteData(c, b)
Chain a function that returns a RemoteData over the success value of a RemoteData
Examples
try(over: Success(1), with: fn(x) { Success(x * 2) })
// -> Success(2)
try(over: Failure("error"), with: fn(x) { Success(x * 2) })
// -> Failure("error")
try(over: Success(1), with: fn(x) { Failure("error") })
// -> Failure("error")