I’m trying to make a `suspend` function execute it...
# coroutines
r
I’m trying to make a
suspend
function execute its contents on a specific dispatcher. So far:``` suspend fun getFoo(): Foo? = coroutineScope { withContext(Dispatchers.IO) { retrieveFoo() } }``` Is this the best way to go about it?
o
I don't think there's a need to open a new
coroutineScope
, just doing
withContext
should work
5
r
Ah, perfect, thanks!
g
Also this is enough:
Copy code
<http://Dispatchers.IO|Dispatchers.IO> { retrieve Foo }
There is invoke operator for Dispatcher
👍 2
🔥 1
5
o
Oh, that's pretty fancy.
e
@gildor is you example equivalent to withContext? Does invoking the Dispatcher propagate the coroutineContext?
g
Yes, this is just a shortcut for withContext(SomeDispatcher)
e
I am trying it right now but can't find the
invoke
operator for
CoroutineDispatcher
in what version is this available? Or is it not part of the core?
g
Autocomplete works not so well with invoke operator, I should create an issue about this, but invoke is a part of the core https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/invoke.html
e
I see, thank you