Skip to content

Instantly share code, notes, and snippets.

@omkar-tenkale
Created May 7, 2023 19:20
Show Gist options
  • Save omkar-tenkale/90e2d033463bbe136b68d8d6542b5dfe to your computer and use it in GitHub Desktop.
Save omkar-tenkale/90e2d033463bbe136b68d8d6542b5dfe to your computer and use it in GitHub Desktop.
import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.intrinsics.createCoroutineUnintercepted
fun main() {
// Define a suspend lambda that returns "Hello World!"
val suspendingLambda: suspend () -> String = suspend {
"Hello World!"
}
// Define a callback object
val completionCallback = object : Continuation<String> {
override val context: CoroutineContext = EmptyCoroutineContext
override fun resumeWith(result: Result<String>) {
// Prints "Hello World!"
println(result.getOrNull())
}
}
// Create the coroutine
val continuation = suspendingLambda.createCoroutineUnintercepted(completionCallback)
// Start the coroutine
continuation.resumeWith(Result.success(Unit))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment