```import kotlinx.coroutines.delay import kotlinx....
# announcements
j
Copy code
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    sequence {
        yield(10)
    }.map {
        foo() // ERROR: Suspension functions can be called only within coroutine body
    }
    Unit
}

suspend fun foo() = delay(1000L)
why this is not possible?
d
A sequence is lazy, as such the lambda you pass to
map
will be executed at some point in the future by whichever thread might be iterating the
Sequence
. There is no possibility to suspend there, because iterating a sequence is not suspending.
j
Ah... understood. thanks 🙂. I should change to Flow.
d
Yes, indeed.
Flow
is kinda like a sequence where the iteration can suspend
❤️ 1