Skip to content

RemoteData

RemoteData: object

Defined in: Core/RemoteData.ts:31

apply: <E2, A>(arg) => <B, E1>(remoteData) => RemoteData<E2 | E1, B>

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

E2 = never

A = unknown

RemoteData<E2, A>

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

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

chain: <E2, A, B>(transform) => <E1>(remoteData) => RemoteData<E2 | E1, B>

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

E2

A

B

(value) => RemoteData<E2, B>

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

RemoteData.map to transform the success value without returning a new RemoteData.

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

filter: {<A, B, E2>(predicate, onFalse): <E1>(remoteData) => RemoteData<E2 | E1, B>; <A, E2>(predicate, onFalse): <E1>(remoteData) => RemoteData<E2 | E1, 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.

<A, B, E2>(predicate, onFalse): <E1>(remoteData) => RemoteData<E2 | E1, B>

A

B

E2

(value) => value is B

(value) => E2

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

<A, E2>(predicate, onFalse): <E1>(remoteData) => RemoteData<E2 | E1, A>

A

E2

(value) => boolean

(value) => E2

<E1>(remoteData) => RemoteData<E2 | E1, 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

from: object

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

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

E

() => E

<A>(maybe) => 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>(result) => 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>(fallback) => <A, E>(remoteData) => 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

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

RemoteData.match to handle all four lifecycle states.

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

is: object

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

Type guard that checks if a RemoteData is Failure.

E

A

RemoteData<E, A>

remoteData is Failure<E>

RemoteData.is.success to check if data loaded successfully.

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

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

Type guard that checks if a RemoteData is Loading.

E

A

RemoteData<E, A>

remoteData is Loading

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

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

Type guard that checks if a RemoteData is NotAsked.

E

A

RemoteData<E, A>

remoteData is NotAsked

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

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

Type guard that checks if a RemoteData is Success.

E

A

RemoteData<E, A>

remoteData is Success<A>

RemoteData.is.failure to check if data loading failed.

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>(transform) => <E>(remoteData) => RemoteData<E, B>

Transforms the success value inside a RemoteData.

A

B

(value) => B

<E>(remoteData) => 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>(transform) => <A>(remoteData) => RemoteData<F, A>

Transforms the error value inside a RemoteData.

E

F

(error) => F

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

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

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

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

E

A

B

(error) => B

() => B

() => B

(value) => B

(remoteData) => B

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

recover: <E1, B, E2>(fallback) => <A>(remoteData) => RemoteData<E2, B | A>

Recovers from a Failure state by providing a fallback RemoteData. The fallback can produce a different success type or resolve with a different error type.

E1

B = never

E2 = never

(error) => RemoteData<E2, B>

<A>(remoteData) => RemoteData<E2, B | A>

pipe(
  RemoteData.make.failure("offline"),
  RemoteData.recover((err) =>
    err === "offline" ? RemoteData.make.success(42) : RemoteData.make.failure(err)
  ),
); // Success(42)

tap: <A>(sideEffect) => <E>(remoteData) => RemoteData<E, A>

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

A

(value) => void

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

RemoteData.tapError to perform a side effect on the failure error.

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

tapError: <E>(sideEffect) => <A>(remoteData) => RemoteData<E, A>

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

E

(error) => void

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

RemoteData.tap to perform a side effect on the success value.

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

to: object

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

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

E

A

RemoteData<E, A>

Maybe<A>

  • RemoteData.to.Result to convert to a Result with an error fallback.
  • RemoteData.from.Maybe to convert a Maybe into a RemoteData.
RemoteData.to.Maybe(RemoteData.make.success(42)); // Some(42)
RemoteData.to.Maybe(RemoteData.make.loading());   // None
RemoteData.to.Maybe(RemoteData.make.failure("e")); // None

Result: <E2>(onNotReady) => <E1, A>(remoteData) => Result<E2 | E1, A>

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

E2

() => E2

<E1, A>(remoteData) => Result<E2 | E1, A>

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