juan torres
08/03/2021, 7:11 AMasync
and withContext(<http://Dispatches.IO|Dispatches.IO>)
to be able to execute blocking code, in particular making multiple IO calls with Spring RestTemplate, in an asynchronous fashion like so:
fun <T> CoroutineScope.myRestTemplateAsync(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T
): Deferred<T> {
return async(context, start) {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) { block() }
}
}
The full description, observations, and code samples are in this SO Postknthmn
08/03/2021, 8:38 AMasync(context + <http://Dispatchers.IO|Dispatchers.IO>)
juan torres
08/04/2021, 7:48 PMknthmn
08/05/2021, 2:41 AMasync()
and then withContext()
. Both versions replace whatever Dispatcher
specified in context
with <http://Dispatchers.IO|Dispatchers.IO>