https://kotlinlang.org logo
Title
e

eygraber

10/08/2019, 1:24 AM
Is there any way to retrieve the dispatcher from a coroutine context?
a

Adam Powell

10/08/2019, 1:56 AM
yes, but there generally isn't a lot of reason to, why?
e

eygraber

10/08/2019, 2:18 AM
I'm wrapping a 3rd party API that expects an executor in a
suspendCancellableCoroutine
. I want to be able to get the dispatcher from the continuation's context and turn that into an executor
a

Adam Powell

10/08/2019, 2:24 AM
thought that might be it. 🙂 You don't need to. Use a directExecutor and just resume the continuation with the right result, it'll dispatch to the right CoroutineDispatcher as part of resuming the continuation
e

eygraber

10/08/2019, 2:31 AM
Is that from Guava?
a

Adam Powell

10/08/2019, 2:51 AM
Yes but you can make your own trivially:
object DirectExecutor : Executor {
  override fun execute(r: Runnable) {
    r.run()
  }
}
e

eygraber

10/08/2019, 3:08 AM
Interesting. Thanks!