diff --git a/packages/fabric/core/result/async-result.ts b/packages/fabric/core/result/async-result.ts index d9a1bfd..dd9db28 100644 --- a/packages/fabric/core/result/async-result.ts +++ b/packages/fabric/core/result/async-result.ts @@ -110,21 +110,35 @@ export class AsyncResult< /** * Map a function over the error of the result. */ - mapError( + errorMap( fn: (error: TError) => TMappedError, ): 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. - * This is useful for side effects that do not modify the result. + * Execute a function if the result is not an error. + * The function does not affect the result. */ tap(fn: (value: TValue) => void): AsyncResult { return new AsyncResult( this.r.then((result) => result.tap(fn)), ); } + + assert( + fn: (value: TValue) => AsyncResult, + ): AsyncResult { + return new AsyncResult( + this.r.then((result) => { + if (result.isError()) { + return result as any; + } + + return (fn(result.unwrapOrThrow())).promise(); + }), + ); + } } diff --git a/packages/fabric/core/result/result.ts b/packages/fabric/core/result/result.ts index dd1fda8..180822e 100644 --- a/packages/fabric/core/result/result.ts +++ b/packages/fabric/core/result/result.ts @@ -127,7 +127,7 @@ export class Result { /** * Map a function over the error of the result. */ - mapError( + errorMap( fn: (error: TError) => TMappedError, ): Result { if (isError(this.value)) { @@ -143,9 +143,19 @@ export class Result { */ tap(fn: (value: TValue) => void): Result { if (!isError(this.value)) { - fn(this.value as TValue); + try { + fn(this.value as TValue); + } catch { + // do nothing + } } return this; } + + assert( + fn: (value: TValue) => Result, + ): Result { + return this.flatMap((value) => fn(value).map(() => value)); + } }