rrva
10/22/2017, 5:35 PMawait
inside a suspendable function, how does that affect the scalability? I did this small test which models my real code to find out
package rrva.mashup
import kotlinx.coroutines.experimental.*
fun fetchBarDeferred(n: Int): Deferred<Int> {
return async(CommonPool) {
delay(1000)
n
}
}
suspend fun fetchFoo(n: Int): Int {
val resp = fetchBarDeferred(n)
return resp.await() * 2
}
fun asyncFetchFoo(n: Int) = async(CommonPool) {
fetchFoo(n)
}
fun main(args: Array<String>) {
runBlocking {
val numbers = (1..10000).map { n -> asyncFetchFoo(n) }.map { deferred -> deferred.await() }
println(numbers.size)
}
}
Looks like await() inside fetchFoo
allows the coroutine to be suspended at that point?