obobo
02/25/2019, 9:48 PMCoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>)
and then uses async
on it to create the Deferreds. My question is whether this is the correct way to do this, or if I should use the coroutineScope
function to create the scope and call async
from within. Unsure if this is a #ktor or #coroutines question, seems more like the former.bdawg.io
02/25/2019, 10:56 PMrouting {
get { // `this` is a CoroutineScope already
// inside of your handler
val one = async(<http://Dispatchers.IO|Dispatchers.IO>) { ... }
val two = async(<http://Dispatchers.IO|Dispatchers.IO>) { ... }
val three = async(<http://Dispatchers.IO|Dispatchers.IO>) { ... }
}
}
this attaches your async
coroutines to be part of your route’s lifecycleasync
builders using with
to reduce duplication of which dispatcher to use routing {
get {
val results = with(CoroutineScope(coroutineContext + <http://Dispatchers.IO|Dispatchers.IO>)) {
val one = async { ... }
val two = async { ... }
val three = async { ... }
listOf(one, two, three)
}.awaitAll()
}
}