Skip to content

Commit 3fc1c20

Browse files
authored
Merge pull request #3651 from Kotlin/tutorial-cancelling-update
update: instructions for coroutine cancelation
2 parents ca14606 + 6e17a55 commit 3fc1c20

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

docs/topics/coroutines-and-channels.md

+28-25
Original file line numberDiff line numberDiff line change
@@ -870,13 +870,33 @@ completion or cancel it explicitly, but that won't happen automatically as it wo
870870
871871
### Canceling the loading of contributors
872872
873-
Consider two versions of the `loadContributorsConcurrent()` function. The first uses `coroutineScope` to start all of the
874-
child coroutines, whereas the second uses `GlobalScope`. Compare how both versions behave when you try to cancel
875-
the parent coroutine.
873+
Create two versions of the function that loads the list of contributors. Compare how both versions behave when you try to
874+
cancel the parent coroutine. The first version will use `coroutineScope` to start all of the child coroutines,
875+
whereas the second will use `GlobalScope`.
876+
877+
1. In `Request5Concurrent.kt`, add a 3-second delay to the `loadContributorsConcurrent()` function:
878+
879+
```kotlin
880+
suspend fun loadContributorsConcurrent(
881+
service: GitHubService,
882+
req: RequestData
883+
): List<User> = coroutineScope {
884+
// ...
885+
async {
886+
log("starting loading for ${repo.name}")
887+
delay(3000)
888+
// load repo contributors
889+
}
890+
// ...
891+
}
892+
```
893+
894+
The delay affects all of the coroutines that send requests, so that there's enough time to cancel the loading
895+
after the coroutines are started but before the requests are sent.
876896

877-
1. Copy the implementation of `loadContributorsConcurrent()` from `Request5Concurrent.kt` to
878-
`loadContributorsNotCancellable()` in `Request5NotCancellable.kt`, and then remove the creation of a new `coroutineScope`.
879-
2. The `async` calls now fail to resolve, so start them by using `GlobalScope.async`:
897+
2. Create the second version of the loading function: copy the implementation of `loadContributorsConcurrent()` to
898+
`loadContributorsNotCancellable()` in `Request5NotCancellable.kt` and then remove the creation of a new `coroutineScope`.
899+
3. The `async` calls now fail to resolve, so start them by using `GlobalScope.async`:
880900

881901
```kotlin
882902
suspend fun loadContributorsNotCancellable(
@@ -894,25 +914,8 @@ the parent coroutine.
894914
```
895915

896916
* The function now returns the result directly, not as the last expression inside the lambda (lines `#1` and `#3`).
897-
* All of the "contributors" coroutines are started inside the `GlobalScope`, not as children of the coroutine scope (
898-
line `#2`).
899-
3. Add a 3-second delay to all of the coroutines that send requests, so that there's enough time to cancel the loading
900-
after the coroutines are started but before the requests are sent:
901-
902-
```kotlin
903-
suspend fun loadContributorsConcurrent(
904-
service: GitHubService,
905-
req: RequestData
906-
): List<User> = coroutineScope {
907-
// ...
908-
async {
909-
log("starting loading for ${repo.name}")
910-
delay(3000)
911-
// load repo contributors
912-
}
913-
// ...
914-
}
915-
```
917+
* All of the "contributors" coroutines are started inside the `GlobalScope`, not as children of the coroutine scope
918+
(line `#2`).
916919

917920
4. Run the program and choose the _CONCURRENT_ option to load the contributors.
918921
5. Wait until all of the "contributors" coroutines are started, and then click _Cancel_. The log shows no new results,

0 commit comments

Comments
 (0)