1- import { isSome } from '.' ;
1+ import { ErrorHandler , isSome } from '.' ;
22import { ResultAsync } from './resultAsync' ;
33import { Unit } from './unit' ;
44import {
@@ -175,7 +175,7 @@ export class Result<TValue = Unit, TError = string> {
175175 */
176176 static try < TValue , TError = string > (
177177 factory : FunctionOfT < Some < TValue > > ,
178- errorHandler : FunctionOfTtoK < unknown , TError >
178+ errorHandler : ErrorHandler < TError >
179179 ) : Result < TValue , TError > ;
180180 /**
181181 * Creates a new successful Result with a Unit value.
@@ -186,7 +186,7 @@ export class Result<TValue = Unit, TError = string> {
186186 */
187187 static try < TError = string > (
188188 action : Action ,
189- errorHandler : FunctionOfTtoK < unknown , TError >
189+ errorHandler : ErrorHandler < TError >
190190 ) : Result < Unit , TError > ;
191191 /**
192192 * Creates a new successful Result with the return value
@@ -198,7 +198,7 @@ export class Result<TValue = Unit, TError = string> {
198198 */
199199 static try < TValue = Unit , TError = string > (
200200 actionOrFactory : FunctionOfT < Some < TValue > > | Action ,
201- errorHandler : FunctionOfTtoK < unknown , Some < TError > >
201+ errorHandler : ErrorHandler < TError >
202202 ) : Result < TValue , TError > {
203203 try {
204204 const value = actionOrFactory ( ) ;
@@ -480,27 +480,31 @@ export class Result<TValue = Unit, TError = string> {
480480 /**
481481 * Maps the value successful Result to a new async value wrapped in a ResultAsync
482482 * @param projection a function given the value of the current Result which returns a Promise of some value
483+ * @param errorHandler a function that converts the error of the rejected Promise to a failed Result
483484 * @returns
484485 */
485486 mapAsync < TNewValue > (
486- projection : FunctionOfTtoK < TValue , Promise < Some < TNewValue > > >
487+ projection : FunctionOfTtoK < TValue , Promise < Some < TNewValue > > > ,
488+ errorHandler ?: ErrorHandler < TError >
487489 ) : ResultAsync < TNewValue , TError > {
488490 return this . isSuccess
489- ? ResultAsync . from ( projection ( this . getValueOrThrow ( ) ) )
491+ ? ResultAsync . from ( projection ( this . getValueOrThrow ( ) ) , errorHandler )
490492 : ResultAsync . failure ( this . getErrorOrThrow ( ) ) ;
491493 }
492494
493495 /**
494496 * Maps the error of a failed Result to a new async value wrapped in a ResultAsync
495497 * @param projection a function given the error of the current Result which returns a Promise of some value
498+ * @param errorHandler a function that converts the error of the rejected Promise to a failed Result
496499 * @returns
497500 */
498501 mapFailureAsync (
499- projection : FunctionOfTtoK < TError , Promise < Some < TValue > > >
502+ projection : FunctionOfTtoK < TError , Promise < Some < TValue > > > ,
503+ errorHandler ?: ErrorHandler < TError >
500504 ) : ResultAsync < TValue , TError > {
501505 return this . isSuccess
502506 ? ResultAsync . from ( this )
503- : ResultAsync . from ( projection ( this . getErrorOrThrow ( ) ) ) ;
507+ : ResultAsync . from ( projection ( this . getErrorOrThrow ( ) ) , errorHandler ) ;
504508 }
505509
506510 /**
@@ -519,9 +523,11 @@ export class Result<TValue = Unit, TError = string> {
519523 /**
520524 * Maps a successful Result to a new ResultAsync
521525 * @param projection
526+ * @param errorHandler a function that converts the error of the rejected Promise to a failed Result
522527 */
523528 bindAsync < TNewValue > (
524- projection : FunctionOfTtoK < TValue , Promise < Result < TNewValue , TError > > >
529+ projection : FunctionOfTtoK < TValue , Promise < Result < TNewValue , TError > > > ,
530+ errorHandler ?: ErrorHandler < TError >
525531 ) : ResultAsync < TNewValue , TError > ;
526532 /**
527533 * Maps a successful Result to a new ResultAsync
@@ -533,12 +539,14 @@ export class Result<TValue = Unit, TError = string> {
533539 /**
534540 * Maps a successful Result to a new ResultAsync
535541 * @param projection
542+ * @param errorHandler a function that converts the error of the rejected Promise to a failed Result
536543 * @returns
537544 */
538545 bindAsync < TNewValue > (
539546 projection :
540547 | FunctionOfTtoK < TValue , Promise < Result < TNewValue , TError > > >
541- | FunctionOfTtoK < TValue , ResultAsync < TNewValue , TError > >
548+ | FunctionOfTtoK < TValue , ResultAsync < TNewValue , TError > > ,
549+ errorHandler ?: ErrorHandler < TError >
542550 ) : ResultAsync < TNewValue , TError > {
543551 if ( this . isFailure ) {
544552 return ResultAsync . failure ( this . getErrorOrThrow ( ) ) ;
@@ -547,7 +555,7 @@ export class Result<TValue = Unit, TError = string> {
547555 const resultAsyncOrPromise = projection ( this . getValueOrThrow ( ) ) ;
548556
549557 return isPromise ( resultAsyncOrPromise )
550- ? ResultAsync . from < TNewValue , TError > ( resultAsyncOrPromise )
558+ ? ResultAsync . from < TNewValue , TError > ( resultAsyncOrPromise , errorHandler )
551559 : resultAsyncOrPromise ;
552560 }
553561
@@ -580,16 +588,23 @@ export class Result<TValue = Unit, TError = string> {
580588 /**
581589 * Executes an async action if the Result succeeded
582590 * @param action a function given the Result's value returns a Promise
591+ * @param errorHandler a function that converts the error of the rejected Promise to a failed Result
583592 * @returns a ResultAsync
584593 */
585- tapAsync ( action : AsyncActionOfT < TValue > ) : ResultAsync < TValue , TError > {
594+ tapAsync (
595+ action : AsyncActionOfT < TValue > ,
596+ errorHandler ?: ErrorHandler < TError >
597+ ) : ResultAsync < TValue , TError > {
586598 if ( this . isFailure ) {
587599 return ResultAsync . failure ( this . getErrorOrThrow ( ) ) ;
588600 }
589601
590602 const value = this . getValueOrThrow ( ) ;
591603
592- return ResultAsync . from ( action ( value ) . then ( ( ) => value ) ) ;
604+ return ResultAsync . from (
605+ action ( value ) . then < Some < TValue > > ( ( ) => value ) ,
606+ errorHandler
607+ ) ;
593608 }
594609
595610 /**
@@ -686,12 +701,12 @@ export class Result<TValue = Unit, TError = string> {
686701 /**
687702 *
688703 * @param action
689- * @param errorCreator
704+ * @param errorHandler
690705 * @returns
691706 */
692707 onSuccessTry (
693708 action : ActionOfT < TValue > ,
694- errorCreator : FunctionOfTtoK < unknown , Some < TError > >
709+ errorHandler : ErrorHandler < TError >
695710 ) : Result < TValue , TError > {
696711 if ( this . isFailure ) {
697712 return this ;
@@ -704,7 +719,7 @@ export class Result<TValue = Unit, TError = string> {
704719
705720 return Result . success ( value ) ;
706721 } catch ( error : unknown ) {
707- return Result . failure ( errorCreator ( error ) ) ;
722+ return Result . failure ( errorHandler ( error ) ) ;
708723 }
709724 }
710725
@@ -716,13 +731,13 @@ export class Result<TValue = Unit, TError = string> {
716731 */
717732 onSuccessTryAsync (
718733 asyncAction : FunctionOfTtoK < TValue , Promise < void > > ,
719- errorHander : FunctionOfTtoK < unknown , Some < TError > >
734+ errorHander : ErrorHandler < TError >
720735 ) : ResultAsync < TValue , TError > {
721736 if ( this . isFailure ) {
722737 return ResultAsync . failure ( this . getErrorOrThrow ( ) ) ;
723738 }
724739
725- const result = async ( ) => {
740+ const promiseFactory = async ( ) => {
726741 const value = this . getValueOrThrow ( ) ;
727742
728743 try {
@@ -734,7 +749,7 @@ export class Result<TValue = Unit, TError = string> {
734749 }
735750 } ;
736751
737- return ResultAsync . from < TValue , TError > ( result ( ) ) ;
752+ return ResultAsync . from < TValue , TError > ( promiseFactory ( ) , errorHander ) ;
738753 }
739754
740755 /**
0 commit comments