Kotlin flow withTimeout repeat

Kotlin flow withTimeout repeat

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onEmpty

fun main() {
    runBlocking {
        val fa = flow {
            withTimeout(5000) {
                repeat(3) {
                    delay(1000)
                    emit(it)
                }
            }
        }.onEach {
            println("a-onEach-$it")
        }.onCompletion {
            println("a onCompletion")
        }.onEmpty {
            println("a onEmpty")
        }.collect {
            println("a-$it")
        }

        val fb = flow {
            withTimeout(2000) {
                repeat(3) {
                    delay(1000)
                    emit(it)
                }
            }
        }.onEach {
            println("b-onEach-$it")
        }.onCompletion {
            println("b onCompletion")
        }.onEmpty {
            println("b onEmpty")
        }.collect {
            println("b-$it")
        }
    }
}

a-onEach-0
a-0
a-onEach-1
a-1
a-onEach-2
a-2
a onCompletion
b-onEach-0
b-0
b onCompletion
Exception in thread "main" kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 2000 ms
    at kotlinx.coroutines.TimeoutKt.TimeoutCancellationException(Timeout.kt:184)
    at kotlinx.coroutines.TimeoutCoroutine.run(Timeout.kt:154)
    at kotlinx.coroutines.EventLoopImplBase$DelayedRunnableTask.run(EventLoop.common.kt:508)
    at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:284)
    at kotlinx.coroutines.DefaultExecutor.run(DefaultExecutor.kt:108)
    at java.base/java.lang.Thread.run(Thread.java:1623)

Process finished with exit code 1

kotlin协程flow filter map flowOn zip combine(1)_zhangphil的博客-CSDN博客一、flow ,emit,onCompletion,collect。四、map,重组改写数据。八、conflate 合并。九、debounce去重。二、函数作为flow。https://blog.csdn.net/zhangphil/article/details/130084723kotlin协程flow retry retryWhen(2)_zhangphil的博客-CSDN博客kotlin协程flow retry retryWhen(2)二、retryWhen。https://blog.csdn.net/zhangphil/article/details/130086523kotlin协程flow retryWhen当功能函数加载失败后重试(3)_zhangphil的博客-CSDN博客kotlin协程flow retryWhen当功能函数加载失败后重试(3)加载一次失败,重试1次成功。https://blog.csdn.net/zhangphil/article/details/130092299kotlin协程flow retry功能函数返回失败后重试(4)_zhangphil的博客-CSDN博客一、flow ,emit,onCompletion,collect。kotlin协程flow retryWhen当功能函数加载失败后重试(3)_zhangphil的博客-CSDN博客。kotlin协程flow retryWhen当功能函数加载失败后重试(3)加载一次失败,重试1次成功。kotlin协程flow retry retryWhen(2)_zhangphil的博客-CSDN博客。kotlin协程flow retry retryWhen(2)二、retryWhen。初次加载失败,重试两次成功。https://blog.csdn.net/zhangphil/article/details/130093111

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/132099928