Hey to all. Quick question. How can I use flow to ...
# coroutines
k
Hey to all. Quick question. How can I use flow to emit a value every X seconds for example ? Is there any API available or do I have to build a Flow myself ?
d
Copy code
fun foo(): Flow<Int> = flow {
    for (i in 1..3) {
        delay(100)
        emit(i)
    }
}
Something like that?
k
Yes exactly that's how I intend to build it myself. Was only asking if there was already an function available where you just put a value generator and a delay duration. But thanks anyways
Or analogous to the
interval
from Rx. But it is astonishingly easy to create that yourself with flow so probably they keep the API small
👍 1
b
You can also use a
Sequence
to generate the numbers and convert it to a flow that's delayed
Copy code
generateSequence(0) { it + 1 }
    .asFlow()
    .delayEach(100)
    ...
or in his example above
Copy code
(1..3).asFlow().delayEach(100)
It ends up coming down to syntax preference and what makes sense for your use case though :)
j
You mean you need a operator like
emitByPeriod()
or something like that ?
👍 1