Skip to content

Commit 8165533

Browse files
committed
readme: catchAndReturn, catchAndResume
1 parent a9f6355 commit 8165533

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ dependencies {
143143
- [`cast`](#cast--castnotnull--castnullable--safeCast)
144144
- [`castNotNull`](#cast--castnotnull--castnullable--safeCast)
145145
- [`castNullable`](#cast--castnotnull--castnullable--safeCast)
146+
- [`catchAndReturn`, `catchAndResume`](#catchAndReturn--catchAndResume)
146147
- [`chunked`](#buffercount--chunked)
147148
- [`safeCast`](#cast--castnotnull--castnullable--safeCast)
148149
- [`concatWith`](#concatwith--plus)
@@ -500,6 +501,68 @@ timer: kotlin.Unit
500501

501502
----
502503

504+
#### catchAndReturn / catchAndResume
505+
506+
- Catches exceptions in the flow completion, and emits a single item or resumes with another flow.
507+
508+
- Similar to
509+
- [RxJava onErrorReturn](https://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Flowable.html#onErrorReturn-io.reactivex.rxjava3.functions.Function-)
510+
- [RxJava onErrorReturnItem](https://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Flowable.html#onErrorReturnItem-T-)
511+
- [RxJava onErrorResumeNext](https://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Flowable.html#onErrorResumeNext-io.reactivex.rxjava3.functions.Function-)
512+
- [RxJava onErrorResumeWith](https://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Flowable.html#onErrorResumeWith-org.reactivestreams.Publisher-)
513+
- [RxSwift catchAndReturn](https://github.com/ReactiveX/RxSwift/blob/551639293147e54fddced6f967a60d115818e18e/RxSwift/Observables/Catch.swift#L46)
514+
515+
```kotlin
516+
flowOf(1, 2)
517+
.concatWith(flow { throw RuntimeException("original error") })
518+
.catchAndReturn(3)
519+
.collect { v: Int -> println("catchAndReturn: $v") }
520+
521+
println("---")
522+
523+
flowOf(1, 2)
524+
.concatWith(flow { throw RuntimeException("original error") })
525+
.catchAndReturn { e: Throwable -> e.message?.length ?: 0 }
526+
.collect { v: Int -> println("catchAndReturn: $v") }
527+
528+
println("---")
529+
530+
flowOf(1, 2)
531+
.concatWith(flow { throw RuntimeException("original error") })
532+
.catchAndResume(flowOf(3, 4))
533+
.collect { v: Int -> println("catchAndResume: $v") }
534+
535+
println("---")
536+
537+
flowOf(1, 2)
538+
.concatWith(flow { throw RuntimeException("original error") })
539+
.catchAndResume { e: Throwable -> flowOf(e.message?.length ?: 0) }
540+
.collect { v: Int -> println("catchAndResume: $v") }
541+
```
542+
543+
Output:
544+
545+
```none
546+
catchAndReturn: 1
547+
catchAndReturn: 2
548+
catchAndReturn: 3
549+
---
550+
catchAndReturn: 1
551+
catchAndReturn: 2
552+
catchAndReturn: 14
553+
---
554+
catchAndResume: 1
555+
catchAndResume: 2
556+
catchAndResume: 3
557+
catchAndResume: 4
558+
---
559+
catchAndResume: 1
560+
catchAndResume: 2
561+
catchAndResume: 14
562+
```
563+
564+
----
565+
503566
##### cast
504567
Adapt this `Flow` to be a `Flow<R>`.
505568

0 commit comments

Comments
 (0)