Created
May 7, 2023 19:20
-
-
Save omkar-tenkale/90e2d033463bbe136b68d8d6542b5dfe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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