Sander Ploegsma
02/25/2019, 7:59 AMsuspend
functions in parallel inside a route? This compiles but crashes at runtime with `io.ktor.util.pipeline.SuspendFunctionGun cannot be cast to kotlinx.coroutines.CoroutineScope`:
fun Routing.test() {
get("/") {
val firstResultDeferred = async { firstCall() }
val secondResultDeferred = async { secondCall() }
val firstResult = firstResultDeferred.await()
val secondResult = secondResultDeferred.await()
call.respond(listOf(firstResult, secondResult))
}
}
gildor
02/25/2019, 8:03 AMSander Ploegsma
02/25/2019, 8:09 AMe5l
02/25/2019, 8:10 AMSander Ploegsma
02/25/2019, 8:22 AM.await()
inside the call.respond()
gildor
02/25/2019, 8:25 AMSander Ploegsma
02/25/2019, 8:29 AMget("/test") {
val a = async { a() }
val b = async { b() }
call.respond(listOf(a.await(), b.await()))
}
EDIT: Oh wait, never mind. In my example it works, but my production code uses call.respondHtmlTemplate
with a block function parameter that is not suspendablecy
02/25/2019, 8:30 AMSuspendFunctionGun
implements the interfaceSander Ploegsma
02/25/2019, 8:32 AMcy
02/25/2019, 8:42 AMSander Ploegsma
02/25/2019, 8:59 AMgildor
02/25/2019, 9:11 AMbut my production code usesYes, it make sense. You can create a feature request to make respondHtmlTemplate suspendable. For now I think the only way is suspend before this block. receive data and than use it in respondHtmlTemplate blockwith a block function parameter that is not suspendablecall.respondHtmlTemplate
Sander Ploegsma
02/25/2019, 9:47 AMio.ktor.server.jetty.EngineMain
everything works as expected, also with hot reloading enabledcy
02/25/2019, 9:55 AMSander Ploegsma
02/25/2019, 9:57 AM