Skip to content

RemoteData

RemoteData: object

Defined in: Core/RemoteData.ts:30

ap: <E, A>(arg) => <B>(data) => RemoteData<E, B>

Applies a function wrapped in a RemoteData to a value wrapped in a RemoteData.

E

A

RemoteData<E, A>

<B>(data) => RemoteData<E, B>

const add = (a: number) => (b: number) => a + b;
pipe(
  RemoteData.make.success(add),
  RemoteData.ap(RemoteData.make.success(5)),
  RemoteData.ap(RemoteData.make.success(3))
); // Success(8)

chain: <E2, A, B>(f) => <E1>(data) => RemoteData<E2 | E1, B>

Chains RemoteData computations. If the input is Success, passes the value to f. Otherwise, propagates the current state.

E2

A

B

(a) => RemoteData<E2, B>

<E1>(data) => RemoteData<E2 | E1, B>

pipe(
  RemoteData.make.success(5),
  RemoteData.chain(n => n > 0 ? RemoteData.make.success(n) : RemoteData.make.failure("negative"))
);

filter: <E, A>(pred, onFalse) => (data) => RemoteData<E, A>

Filters a Success value. When the predicate passes, the value is kept. When it fails, Success becomes Failure using the error produced by onFalse. All other states pass through unchanged.

E

A

(a) => boolean

(a) => E

(data) => RemoteData<E, A>

RemoteData.filter(n => n > 0, n => `${n} is not a valid price`)(RemoteData.make.success(9.99));
// Success(9.99)
RemoteData.filter(n => n > 0, n => `${n} is not a valid price`)(RemoteData.make.success(-1));
// Failure("-1 is not a valid price")
RemoteData.filter(n => n > 0, () => "error")(RemoteData.make.loading()); // Loading

fold: <E, A, B>(onFailure, onNotAsked, onLoading, onSuccess) => (data) => B

Extracts the value from a RemoteData by providing handlers for all four cases.

E

A

B

(e) => B

() => B

() => B

(a) => B

(data) => B

pipe(
  userData,
  RemoteData.fold(
    e => `Error: ${e}`,
    () => "Not asked",
    () => "Loading...",
    value => `Got: ${value}`
  )
);

from: object

Maybe: <E>(onNone) => <A>(data) => RemoteData<E, A>

Converts a Maybe to a RemoteData. Some becomes Success, None becomes Failure using the onNone error producer.

E

() => E

<A>(data) => RemoteData<E, A>

pipe(Maybe.make.some(user), RemoteData.from.Maybe(() => "not found")); // Success(user)
pipe(Maybe.make.none(), RemoteData.from.Maybe(() => "not found"));     // Failure("not found")

Result: <E, A>(data) => RemoteData<E, A>

Converts a Result to a RemoteData. Ok becomes Success, Err becomes Failure.

E

A

Result<E, A>

RemoteData<E, A>

const result = await Task.Result.tryCatch(() => loadUser(), { onError: String })();
setState(RemoteData.from.Result(result)); // Success(user) or Failure(msg)

getOrElse: <B>(defaultValue) => <E, A>(data) => B | A

Returns the success value or a default value if the RemoteData is not Success. The default can be a different type, widening the result to A | B.

B

() => B

<E, A>(data) => B | A

pipe(RemoteData.make.success(5), RemoteData.getOrElse(() => 0)); // 5
pipe(RemoteData.make.loading(), RemoteData.getOrElse(() => 0)); // 0
pipe(RemoteData.make.loading<string, number>(), RemoteData.getOrElse(() => null)); // null — typed as number | null

is: object

failure: <E, A>(data) => data is Failure<E> = isFailure

Type guard that checks if a RemoteData is Failure.

E

A

RemoteData<E, A>

data is Failure<E>

const data = RemoteData.make.failure("Failed");
if (RemoteData.is.failure(data)) {
  console.log(data.error); // "Failed"
}

loading: <E, A>(data) => data is Loading = isLoading

Type guard that checks if a RemoteData is Loading.

E

A

RemoteData<E, A>

data is Loading

const data = RemoteData.make.loading();
if (RemoteData.is.loading(data)) {
  console.log("Data is loading");
}

notAsked: <E, A>(data) => data is NotAsked = isNotAsked

Type guard that checks if a RemoteData is NotAsked.

E

A

RemoteData<E, A>

data is NotAsked

const data = RemoteData.make.notAsked();
if (RemoteData.is.notAsked(data)) {
  console.log("Data fetch not initiated");
}

success: <E, A>(data) => data is Success<A> = isSuccess

Type guard that checks if a RemoteData is Success.

E

A

RemoteData<E, A>

data is Success<A>

const data = RemoteData.make.success(42);
if (RemoteData.is.success(data)) {
  console.log(data.value); // 42
}

make: object

failure: <E>(error) => Failure<E> = makeFailure

Creates a Failure RemoteData with the given error.

E

E

Failure<E>

RemoteData.make.failure("Network error"); // Failure("Network error")

loading: () => Loading = makeLoading

Creates a Loading RemoteData.

Loading

RemoteData.make.loading(); // Loading

notAsked: () => NotAsked = makeNotAsked

Creates a NotAsked RemoteData.

NotAsked

RemoteData.make.notAsked(); // NotAsked

success: <A>(value) => Success<A> = makeSuccess

Creates a Success RemoteData with the given value.

A

A

Success<A>

RemoteData.make.success(42); // Success(42)

map: <A, B>(f) => <E>(data) => RemoteData<E, B>

Transforms the success value inside a RemoteData.

A

B

(a) => B

<E>(data) => RemoteData<E, B>

pipe(RemoteData.make.success(5), RemoteData.map(n => n * 2)); // Success(10)
pipe(RemoteData.make.loading(), RemoteData.map(n => n * 2)); // Loading

mapError: <E, F>(f) => <A>(data) => RemoteData<F, A>

Transforms the error value inside a RemoteData.

E

F

(e) => F

<A>(data) => RemoteData<F, A>

pipe(RemoteData.make.failure("oops"), RemoteData.mapError(e => e.toUpperCase())); // Failure("OOPS")

match: <E, A, B>(cases) => (data) => B

Pattern matches on a RemoteData, returning the result of the matching case.

E

A

B

(e) => B

() => B

() => B

(a) => B

(data) => B

pipe(
  userData,
  RemoteData.match({
    notAsked: () => "Click to load",
    loading: () => "Loading...",
    failure: e => `Error: ${e}`,
    success: user => `Hello, ${user.name}!`
  })
);

recover: <E, B>(fallback) => <A>(data) => RemoteData<E, B | A>

Recovers from a Failure state by providing a fallback RemoteData. The fallback can produce a different success type, widening the result to RemoteData<E, A | B>.

E

B

(e) => RemoteData<E, B>

<A>(data) => RemoteData<E, B | A>

tap: <E, A>(f) => (data) => RemoteData<E, A>

Executes a side effect on the success value without changing the RemoteData.

E

A

(a) => void

(data) => RemoteData<E, A>

pipe(
  RemoteData.make.success(5),
  RemoteData.tap(n => console.log("Value:", n)),
  RemoteData.map(n => n * 2)
);

tapError: <E, A>(f) => (data) => RemoteData<E, A>

Executes a side effect on the failure error without changing the RemoteData. Useful for logging errors.

E

A

(e) => void

(data) => RemoteData<E, A>

pipe(
  RemoteData.make.failure("not found"),
  RemoteData.tapError(e => console.error("fetch failed:", e)),
  RemoteData.map(render)
);

to: object

Maybe: <E, A>(data) => Maybe<A>

Converts a RemoteData to a Maybe. Success becomes Some, all other states become None.

E

A

RemoteData<E, A>

Maybe<A>

Result: <E>(onNotReady) => <A>(data) => Result<E, A>

Converts a RemoteData to a Result. Success becomes Ok, Failure becomes Err. NotAsked and Loading become Err with the provided fallback error.

E

() => E

<A>(data) => Result<E, A>

pipe(
  RemoteData.make.success(42),
  RemoteData.to.Result(() => "not loaded")
); // Ok(42)