https://kotlinlang.org logo
#announcements
Title
# announcements
j

Jaymin.Kim

11/29/2019, 9:17 AM
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

diesieben07

11/29/2019, 9:24 AM
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

Jaymin.Kim

11/29/2019, 9:33 AM
Ah... understood. thanks 🙂. I should change to Flow.
d

diesieben07

11/29/2019, 9:33 AM
Yes, indeed.
Flow
is kinda like a sequence where the iteration can suspend
❤️ 1
2 Views