Skip to content

Commit f0c2c5b

Browse files
glasserqwwdfsad
authored andcommitted
Add CompletableDeferred.completeWith(result: Result<T>)
The naming for this convenience function is inspired by Continuation.resumeWith.
1 parent 835ed4d commit f0c2c5b

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-core.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public final class kotlinx/coroutines/CompletableDeferredKt {
119119
public static final fun CompletableDeferred (Ljava/lang/Object;)Lkotlinx/coroutines/CompletableDeferred;
120120
public static final fun CompletableDeferred (Lkotlinx/coroutines/Job;)Lkotlinx/coroutines/CompletableDeferred;
121121
public static synthetic fun CompletableDeferred$default (Lkotlinx/coroutines/Job;ILjava/lang/Object;)Lkotlinx/coroutines/CompletableDeferred;
122+
public static final fun completeWith (Lkotlinx/coroutines/CompletableDeferred;Ljava/lang/Object;)Z
122123
}
123124

124125
public abstract interface class kotlinx/coroutines/CompletableJob : kotlinx/coroutines/Job {

kotlinx-coroutines-core/common/src/CompletableDeferred.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ public interface CompletableDeferred<T> : Deferred<T> {
4545
public fun completeExceptionally(exception: Throwable): Boolean
4646
}
4747

48+
/**
49+
* Completes this deferred value with the value or exception in the given [result]. Returns `true` if this deferred
50+
* was completed as a result of this invocation and `false` otherwise (if it was already completed).
51+
*
52+
* Subsequent invocations of this function have no effect and always produce `false`.
53+
*
54+
* This function transitions this deferred in the same ways described by [CompletableDeferred.complete] and
55+
* [CompletableDeferred.completeExceptionally].
56+
*/
57+
@ExperimentalCoroutinesApi // since 1.3.2, tentatively until 1.4.0
58+
public fun <T> CompletableDeferred<T>.completeWith(result: Result<T>) = result.fold({ complete(it) }, { completeExceptionally(it) })
59+
4860
/**
4961
* Creates a [CompletableDeferred] in an _active_ state.
5062
* It is optionally a child of a [parent] job.

kotlinx-coroutines-core/common/test/CompletableDeferredTest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ class CompletableDeferredTest : TestBase() {
7979
assertTrue(c.getCompletionExceptionOrNull() is TestException)
8080
}
8181

82+
@Test
83+
fun testCompleteWithResultOK() {
84+
val c = CompletableDeferred<String>()
85+
assertEquals(true, c.completeWith(Result.success("OK")))
86+
checkCompleteOk(c)
87+
assertEquals("OK", c.getCompleted())
88+
assertEquals(false, c.completeWith(Result.success("OK")))
89+
checkCompleteOk(c)
90+
assertEquals("OK", c.getCompleted())
91+
}
92+
93+
@Test
94+
fun testCompleteWithResultException() {
95+
val c = CompletableDeferred<String>()
96+
assertEquals(true, c.completeWith(Result.failure(TestException())))
97+
checkCancelWithException(c)
98+
assertEquals(false, c.completeWith(Result.failure(TestException())))
99+
checkCancelWithException(c)
100+
}
101+
82102
@Test
83103
fun testParentCancelsChild() {
84104
val parent = Job()

0 commit comments

Comments
 (0)