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

rkeazor

09/04/2019, 11:41 AM
When should one use async-await Vs withContext
m

marstran

09/04/2019, 11:48 AM
Calling
withContext
is like doing
await
immediately on the
Deferred
from
async
. Use
async
if you want to do something else while waiting for the
Deferred
to complete.
👍🏼 2
l

louiscad

09/04/2019, 12:09 PM
Not exactly. A crash in the
async
block will cancel the parent scope.
withContext
will only throw and will let you catch without an extra scope.
r

rkeazor

09/04/2019, 12:09 PM
Makes sense , Thanks!
Ahhhhh
I see , thanks @louiscad!
So depending how you want your cancellation will be how you choose one from the other
So depending how you want your cancellation will be how you choose one from the other
l

louiscad

09/04/2019, 1:22 PM
@rkeazor Nope, I'll always using
withContext
if I need to change the dispatcher. The only exception might be using
flowOn
to make a flow run on a specific dispatcher.
async
is for parallelization in a coroutine.
1
👍 1
d

Dominaezzz

09/04/2019, 3:46 PM
cough concurrency not parallelism cough
l

louiscad

09/04/2019, 5:45 PM
I see it as local parallelism. The issue is the connotation of these words that I want to avoid. Concurrency is more loaded of it IMHO.
9 Views