Skip to main content

AsyncData<Result<Ok, Error>>

A AsyncData can contain a Result (e.g. to represent an asynchronous value that can fail). We provide some utility functions to deal with that case without having to unwrap the AsyncData result.

note

You can still use all the regular AsyncData methods. The following helpers simply removes the need to unwrap the contained result.

Methods

.mapOkToResult(f)

AsyncData<Result<A, E>>.mapOkToResult<B, F>(
func: (value: A) => Result<B, F>,
): AsyncData<Result<B, E | F>>

Takes a AsyncData<Result<Ok, Error>> and a f function taking Ok and returning Result<ReturnValue, Error> and returns a new AsyncData<Result<ReturnValue, Error>>

Examples
AsyncData.Done(Result.Ok(3)).mapOkToResult((ok) => {
return Result.Ok(ok * 2);
});
// AsyncData<Result.Ok<6>>

AsyncData.Done(Result.Ok(3)).mapOkToResult((ok) =>
isEven(ok) ? Result.Ok(ok) : Result.Error("Odd number");
);
// AsyncData<Result.Error<"Odd number">>

.mapErrorToResult(f)

AsyncData<Result<A, E>>.mapErrorToResult<B, F>(
func: (value: E) => Result<B, F>,
): AsyncData<Result<A | B, F>>

Takes a AsyncData<Result<Ok, Error>> and a f function taking Error and returning Result<ReturnValue, Error> and returns a new AsyncData<Result<ReturnValue, Error>>

Examples
AsyncData.Done(Result.Error(3)).mapErrorToResult((error) => {
return Result.Ok(ok * 2);
});
// AsyncData<Result.Ok<6>>

AsyncData.Done(Result.Error(3)).mapErrorToResult((error) =>
isEven(error) ? Result.Ok(error) : Result.Error("Odd number");
);
// AsyncData<Result.Error<"Odd number">>

.mapOk(f)

AsyncData<Result<A, E>>.mapOk<B>(
func: (value: A) => B,
): AsyncData<Result<B, E>>

Takes a AsyncData<Result<Ok, Error>> and a f function taking Ok and returning ReturnValue and returns a new AsyncData<Result<ReturnValue, Error>>

Examples
AsyncData.Done(Result.Ok(3)).mapOk((ok) => {
return ok * 2;
});
// AsyncData<Result.Ok<6>>

AsyncData.Done(Result.Error("something")).mapOk((ok) => {
return ok * 2;
});
// AsyncData<Result.Error<"something">>

.mapError(f)

AsyncData<Result<A, E>>.mapError<F>(
func: (value: E) => F,
): AsyncData<Result<A, F>>

Takes a AsyncData<Result<Ok, Error>> and a f function taking Error and returning ReturnValue and returns a new AsyncData<Result<Ok, ReturnValue>>

Examples
AsyncData.Done(Result.Error(3)).mapError((error) => {
return error * 2;
});
// AsyncData<Result.Error<6>>

AsyncData.Done(Result.Ok("something")).mapError((ok) => {
return ok * 2;
});
// AsyncData<Result.Ok<"something">>

.flatMapOk(f)

AsyncData<Result<A, E>>.flatMapOk<B, F>(
func: (value: A) => AsyncData<Result<B, F>>,
): AsyncData<Result<B, E | F>>

Takes a AsyncData<Result<Ok, Error>> and a f function taking Ok returning a AsyncData<Result<ReturnValue, Error>>

Examples
AsyncData.Done(Result.Ok(3)).flatMapOk((ok) =>
AsyncData.Done(Result.Ok(ok * 2)),
);
// AsyncData<Result.Ok<6>>

AsyncData.Done(Result.Ok(3)).flatMapOk((ok) =>
AsyncData.Done(Result.Error("Nope")),
);
// AsyncData<Result.Error<"Nope">>

AsyncData.Done(Result.Error("Error")).flatMapOk((ok) =>
AsyncData.Done(Result.Ok(ok * 2)),
);
// AsyncData<Result.Error<"Error">>

.flatMapError(f)

AsyncData<Result<A, E>>.flatMapError<B, F>(
func: (value: E) => AsyncData<Result<B, F>>,
): AsyncData<Result<A | B, F>>

Takes a AsyncData<Result<Ok, Error>> and a f function taking Error returning a AsyncData<Result<Ok, ReturnValue>>

Examples
AsyncData.Done(Result.Ok(3)).flatMapError((error) =>
AsyncData.Done(Result.Ok(ok * 2)),
);
// AsyncData<Result.Ok<3>>

AsyncData.Done(Result.Error("Error")).flatMapError((error) =>
AsyncData.Done(Result.Error("Nope")),
);
// AsyncData<Result.Error<"Nope">>

AsyncData.Done(Result.Error("Error")).flatMapError((error) =>
AsyncData.Done(Result.Ok(1)),
);
// AsyncData<Result.Ok<1>>

Cheatsheet

MethodInputFunction inputFunction outputReturned value
mapOkToResultAsyncData(Ok(x))xOk(y)AsyncData(Ok(y))
mapOkToResultAsyncData(Ok(x))xError(f)AsyncData(Error(f))
mapOkToResultAsyncData(Error(e))not providednot executedAsyncData(Error(e))
mapErrorToResultAsyncData(Error(e))eOk(y)AsyncData(Ok(y))
mapErrorToResultAsyncData(Error(e))eError(f)AsyncData(Error(f))
mapErrorToResultAsyncData(Ok(x))not providednot executedAsyncData(Ok(x))
mapOkAsyncData(Ok(x))xyAsyncData(Ok(y))
mapOkAsyncData(Error(e))not providednot executedAsyncData(Error(e))
mapErrorAsyncData(Ok(x))not providednot executedAsyncData(Ok(x))
mapErrorAsyncData(Error(e))efAsyncData(Error(f))
flatMapOkAsyncData(Ok(x))xAsyncData(Ok(y))AsyncData(Ok(y))
flatMapOkAsyncData(Ok(x))xAsyncData(Error(f))AsyncData(Error(f))
flatMapOkAsyncData(Error(e))not providednot executedAsyncData(Error(e))
flatMapErrorAsyncData(Ok(x))not providednot executedAsyncData(Ok(x))
flatMapErrorAsyncData(Error(e))eAsyncData(Ok(y))AsyncData(Ok(y))
flatMapErrorAsyncData(Error(e))eAsyncData(Error(f))AsyncData(Error(f))