Skip to content

Added docs on withTimeout asynchrony and its use with resources #2252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2020

Conversation

elizarov
Copy link
Contributor

This is a tricky gotcha that needs additional explanation. There are two examples added, one showing the bad code and explaining why it does not work, and the other showing the correct way to write it.

Fixes #2233

This is a tricky gotcha that needs additional explanation. There are two examples added, one showing the bad code and explaining why it does not work, and the other showing the correct way to write it.

Fixes #2233
Copy link
Collaborator

@qwwdfsad qwwdfsad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

launch {
var resource: Resource? = null // Not acquired yet
try {
withTimeout(60) { // Timeout of 60 ms
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example could be simplified with withTimeoutOrNull to avoid try-finally

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try-finally here is used here because we can crash when "doing something else" in this code and we still need to close the resource after that.

@elizarov elizarov merged commit 20ca97a into develop Oct 8, 2020
@elizarov elizarov deleted the with-timeout-resource-doc branch October 8, 2020 14:16
recheej pushed a commit to recheej/kotlinx.coroutines that referenced this pull request Dec 28, 2020
…in#2252)

This is a tricky gotcha that needs additional explanation. There are two examples added, one showing the bad code and explaining why it does not work, and the other showing the correct way to write it.

Fixes Kotlin#2233
recheej pushed a commit to recheej/kotlinx.coroutines that referenced this pull request Dec 28, 2020
…in#2252)

This is a tricky gotcha that needs additional explanation. There are two examples added, one showing the bad code and explaining why it does not work, and the other showing the correct way to write it.

Fixes Kotlin#2233
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants