[fabric/core] Rename mapError to errorMap; add Result.assert utility function
This commit is contained in:
parent
523578e310
commit
65432d1c54
@ -110,21 +110,35 @@ export class AsyncResult<
|
|||||||
/**
|
/**
|
||||||
* Map a function over the error of the result.
|
* Map a function over the error of the result.
|
||||||
*/
|
*/
|
||||||
mapError<TMappedError extends TaggedError>(
|
errorMap<TMappedError extends TaggedError>(
|
||||||
fn: (error: TError) => TMappedError,
|
fn: (error: TError) => TMappedError,
|
||||||
): AsyncResult<TValue, TMappedError> {
|
): AsyncResult<TValue, TMappedError> {
|
||||||
return new AsyncResult(
|
return new AsyncResult(
|
||||||
this.r.then((result) => result.mapError(fn)),
|
this.r.then((result) => result.errorMap(fn)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Taps a function if the result is a success.
|
* Execute a function if the result is not an error.
|
||||||
* This is useful for side effects that do not modify the result.
|
* The function does not affect the result.
|
||||||
*/
|
*/
|
||||||
tap(fn: (value: TValue) => void): AsyncResult<TValue, TError> {
|
tap(fn: (value: TValue) => void): AsyncResult<TValue, TError> {
|
||||||
return new AsyncResult(
|
return new AsyncResult(
|
||||||
this.r.then((result) => result.tap(fn)),
|
this.r.then((result) => result.tap(fn)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert<TResultValue, TResultError extends TaggedError>(
|
||||||
|
fn: (value: TValue) => AsyncResult<TResultValue, TResultError>,
|
||||||
|
): AsyncResult<TValue, TError | TResultError> {
|
||||||
|
return new AsyncResult(
|
||||||
|
this.r.then((result) => {
|
||||||
|
if (result.isError()) {
|
||||||
|
return result as any;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (fn(result.unwrapOrThrow())).promise();
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -127,7 +127,7 @@ export class Result<TValue, TError extends TaggedError = never> {
|
|||||||
/**
|
/**
|
||||||
* Map a function over the error of the result.
|
* Map a function over the error of the result.
|
||||||
*/
|
*/
|
||||||
mapError<TMappedError extends TaggedError>(
|
errorMap<TMappedError extends TaggedError>(
|
||||||
fn: (error: TError) => TMappedError,
|
fn: (error: TError) => TMappedError,
|
||||||
): Result<TValue, TMappedError> {
|
): Result<TValue, TMappedError> {
|
||||||
if (isError(this.value)) {
|
if (isError(this.value)) {
|
||||||
@ -143,9 +143,19 @@ export class Result<TValue, TError extends TaggedError = never> {
|
|||||||
*/
|
*/
|
||||||
tap(fn: (value: TValue) => void): Result<TValue, TError> {
|
tap(fn: (value: TValue) => void): Result<TValue, TError> {
|
||||||
if (!isError(this.value)) {
|
if (!isError(this.value)) {
|
||||||
|
try {
|
||||||
fn(this.value as TValue);
|
fn(this.value as TValue);
|
||||||
|
} catch {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert<TResultValue, TResultError extends TaggedError>(
|
||||||
|
fn: (value: TValue) => Result<TResultValue, TResultError>,
|
||||||
|
): Result<TValue, TError | TResultError> {
|
||||||
|
return this.flatMap((value) => fn(value).map(() => value));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user