https://kotlinlang.org logo
#android
Title
# android
m

Mostafa Fahimi

03/28/2020, 7:28 AM
Hi. We create a Coroutine Scope, and call a function inside launch. What's the difference between making that function "suspend" and "not suspend(regular)" ?!
l

Luis

03/28/2020, 7:47 AM
suspend functions can call CoroutineContext funtions like
delay()
or
withContext()
. you will have access to the scope of the coroutine inside of that suspend function.
a regular function won’t have access to any of the coroutines magic
m

Mostafa Fahimi

03/28/2020, 7:49 AM
Oh nice. Thanks.
z

Zach Klippenstein (he/him) [MOD]

03/28/2020, 12:29 PM
Whether a function is suspending or takes a scope also implies something about its behavior by convention. If a function has the suspend modifier, it implies it does some asynchronous work and won't return until that work has finished. If a function is not suspending but takes a scope, it implies that function will launch a new coroutine to start some async work and then return immediately. It's an anti-pattern for a function to both have the suspend modifier and take an explicit scope.