Skip to content

Commit b483312

Browse files
committed
Make 'await' to be an extension
1 parent e2fca1a commit b483312

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

kotlinx-coroutines-async-example-ui/src/main/kotlin/main.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private fun createAndShowGUI() {
3131
// 'append' method and consequent 'jProgressBar.setValue' are called
3232
// within Swing event dispatch thread
3333
jTextArea.append(
34-
await(startLongAsyncOperation(i))
34+
startLongAsyncOperation(i).await()
3535
)
3636
jProgressBar.value = i * 10
3737
}

kotlinx-coroutines-async/src/main/kotlin/async.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ fun asyncUI(
9292

9393
typealias ContinuationWrapper = (() -> Unit) -> Unit
9494

95-
suspend fun <V> await(f: CompletableFuture<V>): V =
95+
suspend fun <V> CompletableFuture<V>.await(): V =
9696
runWithCurrentContinuation {
97-
f.whenComplete { value, throwable ->
97+
whenComplete { value, throwable ->
9898
if (throwable == null)
9999
it.resume(value)
100100
else

kotlinx-coroutines-async/src/test/kotlin/AsyncTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class AsyncTest {
1313
@Test
1414
fun testSimple() {
1515
val future = async<String> {
16-
await(CompletableFuture.supplyAsync {
16+
CompletableFuture.supplyAsync {
1717
"O"
18-
}) + "K"
18+
}.await() + "K"
1919
}
2020

2121
assertEquals("OK", future.get())
@@ -25,7 +25,7 @@ class AsyncTest {
2525
fun testWaitForCompletion() {
2626
val toAwait = CompletableFuture<String>()
2727
val future = async<String> {
28-
await(toAwait) + "K"
28+
toAwait.await() + "K"
2929
}
3030

3131
assertFalse(future.isDone)
@@ -39,7 +39,7 @@ class AsyncTest {
3939
val toAwait = CompletableFuture<String>()
4040
val future = async<String> {
4141
try {
42-
await(toAwait)
42+
toAwait.await()
4343
} catch (e: RuntimeException) {
4444
e.message!!
4545
} + "K"
@@ -54,10 +54,10 @@ class AsyncTest {
5454
@Test
5555
fun testExceptionInsideCoroutine() {
5656
val future = async<String> {
57-
if (await(CompletableFuture.supplyAsync { true })) {
57+
if (CompletableFuture.supplyAsync { true }.await()) {
5858
throw IllegalStateException("OK")
5959
}
60-
await(CompletableFuture.supplyAsync { "fail" })
60+
CompletableFuture.supplyAsync { "fail" }.await()
6161
}
6262

6363
try {
@@ -81,21 +81,21 @@ class AsyncTest {
8181
assertEquals(1, depth.get(), "Part before first suspension must be wrapped")
8282

8383
val result =
84-
await(CompletableFuture.supplyAsync {
84+
CompletableFuture.supplyAsync {
8585
while (depth.get() > 0);
8686

8787
assertEquals(0, depth.get(), "Part inside suspension point should not be wrapped")
8888
"OK"
89-
})
89+
}.await()
9090

9191
assertEquals(1, depth.get(), "Part after first suspension should be wrapped")
9292

93-
await(CompletableFuture.supplyAsync {
93+
CompletableFuture.supplyAsync {
9494
while (depth.get() > 0);
9595

9696
assertEquals(0, depth.get(), "Part inside suspension point should not be wrapped")
9797
"ignored"
98-
})
98+
}.await()
9999

100100
result
101101
}

0 commit comments

Comments
 (0)