@@ -36,6 +36,7 @@ For asynchronous tasks, `neverthrow` offers a `ResultAsync` class which wraps a
36
36
- [ ` Result.match ` (method)] ( #resultmatch-method )
37
37
- [ ` Result.asyncMap ` (method)] ( #resultasyncmap-method )
38
38
- [ ` Result.andTee ` (method)] ( #resultandtee-method )
39
+ - [ ` Result.orTee ` (method)] ( #resultortee-method )
39
40
- [ ` Result.andThrough ` (method)] ( #resultandthrough-method )
40
41
- [ ` Result.asyncAndThrough ` (method)] ( #resultasyncandthrough-method )
41
42
- [ ` Result.fromThrowable ` (static class method)] ( #resultfromthrowable-static-class-method )
@@ -55,6 +56,7 @@ For asynchronous tasks, `neverthrow` offers a `ResultAsync` class which wraps a
55
56
- [ ` ResultAsync.orElse ` (method)] ( #resultasyncorelse-method )
56
57
- [ ` ResultAsync.match ` (method)] ( #resultasyncmatch-method )
57
58
- [ ` ResultAsync.andTee ` (method)] ( #resultasyncandtee-method )
59
+ - [ ` ResultAsync.orTee ` (method)] ( #resultasyncortee-method )
58
60
- [ ` ResultAsync.andThrough ` (method)] ( #resultasyncandthrough-method )
59
61
- [ ` ResultAsync.combine ` (static class method)] ( #resultasynccombine-static-class-method )
60
62
- [ ` ResultAsync.combineWithAllErrors ` (static class method)] ( #resultasynccombinewithallerrors-static-class-method )
@@ -593,6 +595,53 @@ resAsync.then((res: Result<void, ParseError | InsertError>) => {e
593
595
594
596
---
595
597
598
+ #### ` Result.orTee ` (method)
599
+
600
+ Like ` andTee ` for the error track. Takes a ` Result<T, E> ` and lets the ` Err ` value pass through regardless the result of the passed-in function.
601
+ This is a handy way to handle side effects whose failure or success should not affect your main logics such as logging.
602
+
603
+ ** Signature:**
604
+
605
+ ``` typescript
606
+ class Result <T , E > {
607
+ orTee(
608
+ callback : (value : E ) => unknown
609
+ ): Result <T , E > { ... }
610
+ }
611
+ ```
612
+
613
+ ** Example:**
614
+
615
+ ``` typescript
616
+ import { parseUserInput } from ' imaginary-parser'
617
+ import { logParseError } from ' imaginary-logger'
618
+ import { insertUser } from ' imaginary-database'
619
+
620
+ // ^ assume parseUserInput, logParseError and insertUser have the following signatures:
621
+ // parseUserInput(input: RequestData): Result<User, ParseError>
622
+ // logParseError(parseError: ParseError): Result<void, LogError>
623
+ // insertUser(user: User): ResultAsync<void, InsertError>
624
+ // Note logParseError returns void upon success but insertUser takes User type.
625
+
626
+ const resAsync = parseUserInput (userInput )
627
+ .orTee (logParseError )
628
+ .asyncAndThen (insertUser )
629
+
630
+ // Note no LogError shows up in the Result type
631
+ resAsync .then ((res : Result <void , ParseError | InsertError >) => {e
632
+ if (res .isErr ()){
633
+ console .log (" Oops, at least one step failed" , res .error )
634
+ }
635
+ else {
636
+ console .log (" User input has been parsed and inserted successfully." )
637
+ }
638
+ }))
639
+ ```
640
+
641
+ [ ⬆️ Back to top] ( #toc )
642
+
643
+ ---
644
+
596
645
#### ` Result.andThrough ` (method)
597
646
598
647
Similar to ` andTee ` except for:
@@ -1277,6 +1326,53 @@ resAsync.then((res: Result<void, InsertError | NotificationError>) => {e
1277
1326
1278
1327
[ ⬆️ Back to top] ( #toc )
1279
1328
1329
+ ---
1330
+ #### ` ResultAsync.orTee ` (method)
1331
+
1332
+ Like ` andTee ` for the error track. Takes a ` ResultAsync<T, E> ` and lets the original ` Err ` value pass through regardless
1333
+ the result of the passed-in function.
1334
+ This is a handy way to handle side effects whose failure or success should not affect your main logics such as logging.
1335
+
1336
+ ** Signature:**
1337
+
1338
+ ``` typescript
1339
+ class ResultAsync <T , E > {
1340
+ orTee(
1341
+ callback : (value : E ) => unknown
1342
+ ): ResultAsync <T , E > => { ... }
1343
+ }
1344
+ ```
1345
+
1346
+ ** Example:**
1347
+
1348
+ ``` typescript
1349
+ import { insertUser } from ' imaginary-database'
1350
+ import { logInsertError } from ' imaginary-logger'
1351
+ import { sendNotification } from ' imaginary-service'
1352
+
1353
+ // ^ assume insertUser, logInsertError and sendNotification have the following signatures:
1354
+ // insertUser(user: User): ResultAsync<User, InsertError>
1355
+ // logInsertError(insertError: InsertError): Result<void, LogError>
1356
+ // sendNotification(user: User): ResultAsync<void, NotificationError>
1357
+ // Note logInsertError returns void on success but sendNotification takes User type.
1358
+
1359
+ const resAsync = insertUser (user )
1360
+ .orTee (logUser )
1361
+ .andThen (sendNotification )
1362
+
1363
+ // Note there is no LogError in the types below
1364
+ resAsync .then ((res : Result <void , InsertError | NotificationError >) => {e
1365
+ if (res .isErr ()){
1366
+ console .log (" Oops, at least one step failed" , res .error )
1367
+ }
1368
+ else {
1369
+ console .log (" User has been inserted and notified successfully." )
1370
+ }
1371
+ }))
1372
+ ```
1373
+
1374
+ [ ⬆️ Back to top] ( #toc )
1375
+
1280
1376
---
1281
1377
#### ` ResultAsync.andThrough ` (method)
1282
1378
0 commit comments