``` val result = async(myContext) { doSomething() ...
# coroutines
b
Copy code
val result = async(myContext) { doSomething() }.await()
// vs
val result = withContext(myContext) { doSomething() }
i
In this case you should go with the
WithContext
the
async
is useful for parallelism and to cancel the coroutine
💯 1
w
Intellij even has an intention to convert from immediately awaiting over to
withContext
v
the rule of thumb: if you are not sure, use
withContext
🙂
3
i
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
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
Depending on when you read about coroutines you probably read about
run
which was basically renamed to
withContext
v
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
Yes. You can use the for “async lazy”