Zeyad Gasser
Zeyad Gasser

Reputation: 1546

How to merge to kotlin coroutines together?

I have 2 flows that i would like to merge like i used to do it in RxJava.

In Rx-Java:

Flowable.just(1).mergeWith(Flowable.just(2)).subscribe({ println(it)}) // result: 1, 2

How to replicate that in Kotlin Coroutines ? Thanks in advance.

Upvotes: 2

Views: 3027

Answers (1)

John O'Reilly
John O'Reilly

Reputation: 10350

flattenMerge should provide same behaviour. For example,

    val flow1 = (1..3).asFlow()
    val flow2 = (4..6).asFlow()
    flowOf(flow1, flow2).flattenMerge().collect { value ->
        println("$value")
    }

Upvotes: 3

Related Questions