Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow.timeout documentation suggests generic catch {} operator. #3789

Closed
steve-the-edwards opened this issue Jun 22, 2023 · 0 comments
Closed
Labels
bug docs KDoc and API reference

Comments

@steve-the-edwards
Copy link
Contributor

The API documentation for Flow<T>.timeout suggests using a catch{} without rethrowing:

flow {
    emit(1)
    delay(100)
    emit(2)
    delay(100)
    emit(3)
    delay(1000)
    emit(4)
}.timeout(100.milliseconds).catch {
    emit(-1) // Item to emit on timeout
}.onEach {
    delay(300) // This will not cause a timeout
}

This will emit -1 for any and all upstream exceptions (admittedly none other possible in this fizz buzz example). Can we update the documentation to be more targeted?

Such as this.

flow {
    emit(1)
    delay(100)
    emit(2)
    delay(100)
    emit(3)
    delay(1000)
    emit(4)
}.timeout(100.milliseconds).catch { exception ->
    if (exception is TimeoutCancellationException) {
      // Catch the TimeoutCancellationException emitted above.
      emit(-1) // Item to emit on timeout
    } else {
      throw exception
    }
}.onEach {
    delay(300) // This will not cause a timeout
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug docs KDoc and API reference
Projects
None yet
Development

No branches or pull requests

2 participants