https://kotlinlang.org logo
#coroutines
Title
# coroutines
b

bdawg.io

08/14/2018, 5:19 PM
Copy code
val result = async(myContext) { doSomething() }.await()
// vs
val result = withContext(myContext) { doSomething() }
i

igorvd

08/14/2018, 5:29 PM
In this case you should go with the
WithContext
the
async
is useful for parallelism and to cancel the coroutine
💯 1
w

withoutclass

08/14/2018, 5:35 PM
Intellij even has an intention to convert from immediately awaiting over to
withContext
v

Vsevolod Tolstopyatov [JB]

08/14/2018, 6:01 PM
the rule of thumb: if you are not sure, use
withContext
🙂
3
i

igorvd

08/14/2018, 6:57 PM
I don't know how the docs are, but when I was learning coroutines, I don't remember reading anything that mentions the
withContext
.. What I remember is the docs encouraging you to use
async-await
when you want an asynchronous result
e

elizarov

08/14/2018, 7:45 PM
You use
async-await
for concurrent decomposition (do many async and wait for all them). If you want asynchronous result, you just invoke a suspending function.
u

uli

08/14/2018, 7:51 PM
Depending on when you read about coroutines you probably read about
run
which was basically renamed to
withContext
v

v0ldem0rt

08/14/2018, 7:59 PM
Async results can be used by many coroutines in parallel as well; you can achieve cool stuff like https://blog.doordash.com/avoiding-cache-stampede-at-doordash-55bbf596d94b
👍 1
e

elizarov

08/15/2018, 6:29 AM
Yes. You can use the for “async lazy”
2 Views