Hello everyone 👋
I’m wondering if there is any merit or risks in combining
async
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 Post