Converting Callback-based SDK methods to Coroutines in Kotlin

Bevan Steele
Rock and Null
Published in
Jul 7, 2023

Kotlin’s coroutines are an excellent feature of the language that makes asynchronous programming enjoyable and easy to understand.

However, even if you have organized your project neatly and extensively use coroutines, you may encounter a third-party library at some point that still uses old-fashioned callbacks. Combining callbacks and coroutines can be unpleasant, to say the least.

Luckily, there is a solution to this problem. You can wrap these callbacks and “transform” them into coroutines. It’s generally a good practice to wrap your external dependencies and provide a consistent coroutine-style interface, regardless of the interface offered by the third-party libraries.

Continue reading…

--

--