You can rewrite your bar
function like this:
fun bar(completion: () -> Unit) {
GlobalScope.launch(Dispatchers.IO) {
suspendCoroutine<Unit> {
val lambda = {
it.resume(Unit)
}
foo(lambda)
}
withContext(Dispatchers.Main) {
completion()
}
}
}