CompletableDeferred
Object that aims to have value in the future.
Can have the value added into it manually.
Can be awaited on until it has value or has been marked as failed to get value (exception).
Examples
GT-Sandbox-Snapshot: happy path completion (value is returned on await()).
Code
package com.glassthought.sandbox
import com.glassthought.sandbox.util.out.impl.out
import kotlinx.coroutines.*
import kotlin.time.Duration.Companion.milliseconds
fun main(): kotlin.Unit = runBlocking(CoroutineName("main-runBlocking")) {
supervisorScope {
val completable = CompletableDeferred<String>()
launch(CoroutineName("completes completable")) {
out.delay(500.milliseconds, "delay before completing completable exceptionally")
completable.complete("expected-result")
}
out.info("Starting to await completable")
out.info("Result: ${completable.await()}")
}
}
Command to reproduce:
gt.sandbox.checkout.commit 1a4e37e242c352b3e926 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew run --quiet"
Recorded output of command:
Picked up JAVA_TOOL_OPTIONS: -Dkotlinx.coroutines.debug
Picked up JAVA_TOOL_OPTIONS: -Dkotlinx.coroutines.debug
[INFO][elapsed: 15ms][①][coroutname:@main-runBlocking#1] Starting to await completable
[INFO][elapsed: 32ms][⓶][coroutname:@completes completable#2] [🐢] Delaying for [500ms] what_for=[delay before completing completable exceptionally]
[INFO][elapsed: 534ms][⓶][coroutname:@completes completable#2] Done delaying for [500ms] what_for=[delay before completing completable exceptionally]
[INFO][elapsed: 535ms][①][coroutname:@main-runBlocking#1] Result: expected-result
GT-Sandbox-Snapshot: Completing with exception (exception is rethrown on await()).
Code
package com.glassthought.sandbox
import com.glassthought.sandbox.util.out.impl.out
import kotlinx.coroutines.*
import java.lang.Exception
import kotlin.time.Duration.Companion.milliseconds
fun main(): kotlin.Unit = runBlocking(CoroutineName("main-runBlocking")) {
supervisorScope {
val completable = CompletableDeferred<String>()
launch(CoroutineName("completes completable")) {
out.delay(500.milliseconds, "delay before completing completable exceptionally")
completable.completeExceptionally(CancellationException("test exception"))
}
out.info("Starting to await completable")
try {
completable.await()
} catch (e: Exception) {
out.info("Complete exceptionally caught an exception: $e, type: ${e::class.simpleName}")
}
}
}
Command to reproduce:
gt.sandbox.checkout.commit 5cee83f192c53bc638e4 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew run --quiet"
Recorded output of command:
Picked up JAVA_TOOL_OPTIONS: -Dkotlinx.coroutines.debug
Picked up JAVA_TOOL_OPTIONS: -Dkotlinx.coroutines.debug
[INFO][elapsed: 15ms][①][coroutname:@main-runBlocking#1] Starting to await completable
[INFO][elapsed: 32ms][⓶][coroutname:@completes completable#2] [🐢] Delaying for [500ms] what_for=[delay before completing completable exceptionally]
[INFO][elapsed: 533ms][⓶][coroutname:@completes completable#2] Done delaying for [500ms] what_for=[delay before completing completable exceptionally]
[INFO][elapsed: 580ms][①][coroutname:@main-runBlocking#1] Complete exceptionally caught an exception: java.util.concurrent.CancellationException: test exception, type: CancellationException