Skip to content

Instantly share code, notes, and snippets.

@noseratio
Created August 13, 2021 05:05
Show Gist options
  • Save noseratio/636202e1bbf4ca66f516e7253231a045 to your computer and use it in GitHub Desktop.
Save noseratio/636202e1bbf4ca66f516e7253231a045 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.*
suspend fun suspendableDelay(ms: Long): Long {
delay(ms);
return ms;
}
fun regularDelay(ms: Long): Deferred<Long> {
val d = CompletableDeferred<Long>()
GlobalScope.async { delay(ms); d.complete(ms) }
return d;
}
suspend fun test(ms: Long): Long {
delay(ms);
return ms;
}
fun main() {
val r1 = runBlocking { suspendableDelay(250) }
println("suspendableDelay ended: $r1");
val r2 = runBlocking { regularDelay(500).await() }
println("regularDelay ended: $r2");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment