An asynchronous function with a flexible dispatcher

We can bring back some flexibility to this function by making the dispatcher an optional parameter of the function:

private val defDsp = newSingleThreadContext(name = "ServiceCall")
private fun asyncLoadNews(dispatcher: CoroutineDispatcher = defDsp) =
launch(dispatcher) {
...
}

This approach has more flexibility, since now the caller can use a specific CoroutineDispatcher to run this code, but it still has the downside of being explicit only if an appropriate name is given to the function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.106.135