Jaymin.Kim
11/29/2019, 9:17 AMimport 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?diesieben07
11/29/2019, 9:24 AMmap
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.Jaymin.Kim
11/29/2019, 9:33 AMdiesieben07
11/29/2019, 9:33 AMFlow
is kinda like a sequence where the iteration can suspend