Skip to content

Commit fac516f

Browse files
committed
Get rid of awaitAll() in documentation, rewrite currentScope section
Fixes #624
1 parent b63fd82 commit fac516f

File tree

5 files changed

+7
-43
lines changed

5 files changed

+7
-43
lines changed

core/kotlinx-coroutines-core/test/guide/example-compose-05.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ suspend fun doSomethingUsefulTwo(): Int {
2121
suspend fun concurrentSum(): Int = coroutineScope {
2222
val one = async { doSomethingUsefulOne() }
2323
val two = async { doSomethingUsefulTwo() }
24-
awaitAll(one, two)
2524
one.await() + two.await()
2625
}
2726

core/kotlinx-coroutines-core/test/guide/example-compose-06.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,5 @@ suspend fun failedConcurrentSum(): Int = coroutineScope {
3939
println("Second child throws an exception")
4040
throw ArithmeticException()
4141
}
42-
43-
awaitAll(one, two)
44-
one.await() + two.await()
42+
one.await() + two.await()
4543
}

core/kotlinx-coroutines-core/test/guide/test/BasicsGuideTest.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,6 @@ class BasicsGuideTest {
6363
)
6464
}
6565

66-
@Test
67-
fun testKotlinxCoroutinesExperimentalGuideBasic05s() {
68-
test("KotlinxCoroutinesExperimentalGuideBasic05s") { kotlinx.coroutines.experimental.guide.basic05s.main(emptyArray()) }.verifyLines(
69-
"Hello,",
70-
"World!"
71-
)
72-
}
73-
7466
@Test
7567
fun testKotlinxCoroutinesExperimentalGuideBasic06() {
7668
test("KotlinxCoroutinesExperimentalGuideBasic06") { kotlinx.coroutines.experimental.guide.basic06.main(emptyArray()) }.also { lines ->

docs/basics.md

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -310,32 +310,10 @@ World!
310310
But what if the extracted function contains a coroutine builder which is invoked on the current scope?
311311
In this case `suspend` modifier on the extracted function is not enough. Making `doWorld` extension
312312
method on `CoroutineScope` is one of the solutions, but it may not always be applicable as it does not make API clearer.
313-
[currentScope] builder comes to help: it inherits current [CoroutineScope] from the coroutine context it is invoked.
314-
315-
<div class="sample" markdown="1" theme="idea" data-highlight-only>
316-
317-
```kotlin
318-
fun main(args: Array<String>) = runBlocking {
319-
launchDoWorld()
320-
println("Hello,")
321-
}
322-
323-
// this is your first suspending function
324-
suspend fun launchDoWorld() = currentScope {
325-
launch {
326-
println("World!")
327-
}
328-
}
329-
```
330-
331-
</div>
332-
333-
> You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-basic-05s.kt)
334-
335-
<!--- TEST
336-
Hello,
337-
World!
338-
-->
313+
Idiomatic solution is to have either explicit `CoroutineScope` as a field in a class containing target function
314+
or implicit when outer class implements `CoroutineScope`.
315+
As a last resort, [CoroutineScope(coroutineContext)][CoroutineScope()] can be used, but such approach is structurally unsafe
316+
because you no longer have control on the scope this method is executed. Only private API can use this builder.
339317

340318
### Coroutines ARE light-weight
341319

@@ -408,7 +386,7 @@ Active coroutines that were launched in [GlobalScope] do not keep the process al
408386
[Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/index.html
409387
[Job.join]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/join.html
410388
[coroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/coroutine-scope.html
411-
[currentScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/current-scope.html
389+
[CoroutineScope()]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-scope.html
412390
<!--- END -->
413391

414392

docs/composing-suspending-functions.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ scope and that is what [coroutineScope] function provides:
267267
suspend fun concurrentSum(): Int = coroutineScope {
268268
val one = async { doSomethingUsefulOne() }
269269
val two = async { doSomethingUsefulTwo() }
270-
awaitAll(one, two)
271270
one.await() + two.await()
272271
}
273272
```
@@ -327,9 +326,7 @@ suspend fun failedConcurrentSum(): Int = coroutineScope {
327326
println("Second child throws an exception")
328327
throw ArithmeticException()
329328
}
330-
331-
awaitAll(one, two)
332-
one.await() + two.await()
329+
one.await() + two.await()
333330
}
334331
```
335332

0 commit comments

Comments
 (0)