1- import { ErrorHandler } from '.' ;
21import { Result } from './result' ;
32import { Unit } from './unit' ;
43import {
@@ -31,41 +30,31 @@ export class ResultAsync<TValue = Unit, TError = string> {
3130 /**
3231 * Creates a new ResultAsync from the given Promise
3332 * @param value a Promise resolving to a Result
34- * @param errorHandler
3533 */
3634 static from < TValue , TError > (
37- value : Promise < Result < TValue , TError > > ,
38- errorHandler ?: ErrorHandler < TError >
35+ value : Promise < Result < TValue , TError > >
3936 ) : ResultAsync < TValue , TError > ;
4037 /**
4138 * Creates a new ResultAsync from the given Promise
4239 * @param value a Promise which will be converted into a successful Result if it resolves
4340 * and a failed Result if it rejects
4441 */
4542 static from < TValue , TError > (
46- value : Promise < Some < TValue > > ,
47- errorHandler ?: ErrorHandler < TError >
43+ value : Promise < Some < TValue > >
4844 ) : ResultAsync < TValue , TError > ;
4945
5046 static from < TValue , TError > (
5147 value :
5248 | Result < TValue , TError >
5349 | Promise < Result < TValue , TError > >
54- | Promise < Some < TValue > > ,
55- errorHandler ?: ErrorHandler < TError >
50+ | Promise < Some < TValue > >
5651 ) : ResultAsync < TValue , TError > {
5752 if ( isPromise ( value ) ) {
58- let promise = value . then ( ( v ) =>
59- v instanceof Result ? v : Result . success < TValue , TError > ( v )
53+ return new ResultAsync (
54+ value . then ( ( v ) =>
55+ v instanceof Result ? v : Result . success < TValue , TError > ( v )
56+ )
6057 ) ;
61-
62- if ( isFunction ( errorHandler ) ) {
63- promise = promise . catch ( ( error ) =>
64- Result . failure < TValue , TError > ( errorHandler ( error ) )
65- ) ;
66- }
67-
68- return new ResultAsync ( promise ) ;
6958 } else if ( value instanceof Result ) {
7059 return new ResultAsync ( Promise . resolve ( value ) ) ;
7160 }
@@ -293,24 +282,24 @@ export class ResultAsync<TValue = Unit, TError = string> {
293282
294283 /**
295284 * Converts the inner value of a successful Result to a new value.
296- * If the Result failed, no action is taken
297- * @param projection function which accepts the current value as a parameter and returns a new value
285+ * @param projection function which accepts the current value as a parameter and returns a Promise containing
286+ * the new value
298287 */
299288 map < TNewValue > (
300- projection : FunctionOfTtoK < TValue , Some < TNewValue > >
289+ projection : FunctionOfTtoK < TValue , Promise < Some < TNewValue > > >
301290 ) : ResultAsync < TNewValue , TError > ;
302291 /**
303292 * Converts the inner value of a successful Result to a new value.
304- * @param projection function which accepts the current value as a parameter and returns a Promise containing
305- * the new value
293+ * If the Result failed, no action is taken
294+ * @param projection function which accepts the current value as a parameter and returns a new value
306295 */
307296 map < TNewValue > (
308- projection : FunctionOfTtoK < TValue , Promise < Some < TNewValue > > >
297+ projection : FunctionOfTtoK < TValue , Some < TNewValue > >
309298 ) : ResultAsync < TNewValue , TError > ;
310299 map < TNewValue > (
311300 projection :
312- | FunctionOfTtoK < TValue , Some < TNewValue > >
313301 | FunctionOfTtoK < TValue , Promise < Some < TNewValue > > >
302+ | FunctionOfTtoK < TValue , Some < TNewValue > >
314303 ) : ResultAsync < TNewValue , TError > {
315304 return new ResultAsync (
316305 this . value . then ( ( r ) => {
@@ -329,16 +318,20 @@ export class ResultAsync<TValue = Unit, TError = string> {
329318 ) ;
330319 }
331320
321+ /**
322+ *
323+ * @param projection
324+ */
332325 mapError < TNewError > (
333- projection : FunctionOfTtoK < TError , Some < TNewError > >
326+ projection : FunctionOfTtoK < TError , Promise < Some < TNewError > > >
334327 ) : ResultAsync < TValue , TNewError > ;
335328 mapError < TNewError > (
336- projection : FunctionOfTtoK < TError , Promise < Some < TNewError > > >
329+ projection : FunctionOfTtoK < TError , Some < TNewError > >
337330 ) : ResultAsync < TValue , TNewError > ;
338331 mapError < TNewError > (
339332 projection :
340- | FunctionOfTtoK < TError , Some < TNewError > >
341333 | FunctionOfTtoK < TError , Promise < Some < TNewError > > >
334+ | FunctionOfTtoK < TError , Some < TNewError > >
342335 ) : ResultAsync < TValue , TNewError > {
343336 return new ResultAsync (
344337 this . value . then ( ( r ) => {
@@ -357,6 +350,43 @@ export class ResultAsync<TValue = Unit, TError = string> {
357350 ) ;
358351 }
359352
353+ mapFailure (
354+ projection : FunctionOfTtoK < TError , Promise < Some < TValue > > >
355+ ) : ResultAsync < TValue , TError > ;
356+ /**
357+ * Converts a failed Result into a successful one
358+ * @param projection a function that maps the error of the current Result to a value
359+ * @returns A successful Result using the current Result's value if it succeeded and the projection's value if it failed
360+ */
361+ mapFailure (
362+ projection : FunctionOfTtoK < TError , Some < TValue > >
363+ ) : ResultAsync < TValue , TError > ;
364+ mapFailure (
365+ projection :
366+ | FunctionOfTtoK < TError , Promise < Some < TValue > > >
367+ | FunctionOfTtoK < TError , Some < TValue > >
368+ ) : ResultAsync < TValue , TError > {
369+ return new ResultAsync (
370+ this . value . then ( ( r ) => {
371+ if ( r . isSuccess ) {
372+ return r ;
373+ }
374+
375+ const valueOrPromise = projection ( r . getErrorOrThrow ( ) ) ;
376+
377+ if ( isPromise ( valueOrPromise ) ) {
378+ return valueOrPromise . then ( ( v ) => Result . success ( v ) ) ;
379+ }
380+
381+ return Result . success ( valueOrPromise ) ;
382+ } )
383+ ) ;
384+ }
385+
386+ /**
387+ *
388+ * @param projection
389+ */
360390 bind < TNewValue > (
361391 projection : FunctionOfTtoK < TValue , Result < TNewValue , TError > >
362392 ) : ResultAsync < TNewValue , TError > ;
@@ -385,13 +415,17 @@ export class ResultAsync<TValue = Unit, TError = string> {
385415 ) ;
386416 }
387417
388- tap ( action : ActionOfT < TValue > ) : ResultAsync < TValue , TError > ;
418+ /**
419+ *
420+ * @param action
421+ */
389422 tap (
390423 asyncAction : FunctionOfTtoK < TValue , Promise < void > >
391424 ) : ResultAsync < TValue , TError > ;
392425 tap < TOtherValue > (
393426 asyncAction : FunctionOfTtoK < TValue , ResultAsync < TOtherValue , TError > >
394427 ) : ResultAsync < TValue , TError > ;
428+ tap ( action : ActionOfT < TValue > ) : ResultAsync < TValue , TError > ;
395429 tap < TOtherValue > (
396430 action :
397431 | ActionOfT < TValue >
@@ -421,21 +455,29 @@ export class ResultAsync<TValue = Unit, TError = string> {
421455 ) ;
422456 }
423457
458+ /**
459+ *
460+ * @param conditionOrPredicate
461+ * @param action
462+ * @returns
463+ */
424464 tapIf (
425465 conditionOrPredicate : boolean | PredicateOfT < TValue > ,
426466 action : ActionOfT < TValue >
427467 ) : ResultAsync < TValue , TError > {
428468 return new ResultAsync (
429469 this . value . then ( ( r ) => {
430- if ( isFunction ( conditionOrPredicate ) ) {
431- return r . tapIf ( conditionOrPredicate , action ) ;
432- }
433-
434- return r . tapIf ( conditionOrPredicate , action ) ;
470+ return isFunction ( conditionOrPredicate )
471+ ? r . tapIf ( conditionOrPredicate , action )
472+ : r . tapIf ( conditionOrPredicate , action ) ;
435473 } )
436474 ) ;
437475 }
438476
477+ /**
478+ *
479+ * @param matcher
480+ */
439481 match < TNewValue > (
440482 matcher : ResultMatcher < TValue , TError , TNewValue >
441483 ) : Promise < Some < TNewValue > > ;
@@ -448,15 +490,20 @@ export class ResultAsync<TValue = Unit, TError = string> {
448490 return this . value . then ( ( r ) => r . match ( matcher ) ) ;
449491 }
450492
493+ /**
494+ *
495+ * @param projection
496+ * @returns
497+ */
451498 finally < TNewValue > (
452499 projection : FunctionOfTtoK < Result < TValue , TError > , Some < TNewValue > >
453500 ) : Promise < Some < TNewValue > > {
454501 return this . value . then ( ( r ) => r . finally ( projection ) ) ;
455502 }
456503
457- onFailure ( action : ActionOfT < TError > ) : ResultAsync < TValue , TError > ;
458- onFailure ( action : AsyncActionOfT < TError > ) : ResultAsync < TValue , TError > ;
459- onFailure (
504+ tapFailure ( action : ActionOfT < TError > ) : ResultAsync < TValue , TError > ;
505+ tapFailure ( action : AsyncActionOfT < TError > ) : ResultAsync < TValue , TError > ;
506+ tapFailure (
460507 action : ActionOfT < TError > | AsyncActionOfT < TError >
461508 ) : ResultAsync < TValue , TError > {
462509 return new ResultAsync (
0 commit comments