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

Rohan Maity

12/12/2018, 1:03 AM
Hello ! Can somebody tell the difference between Async/Await in C# , JS and Async coroutines in Kotlin. I read the docs of both (Async/await in c# & Async in Kotlin ) . Both lets you do the asynchronous programming without blocking the thread. So except the syntax . Is there any difference? Is there any upper hand of Async coroutine in Kotlin
s

serebit

12/12/2018, 1:06 AM
Copy code
async { suspendingFunction() }.await()
Coroutines in Kotlin have a lot more flexibility and a larger feature set than basic async/await in JS
g

gildor

12/12/2018, 1:39 AM
Probably not the best example of async/await usage %)
Rohan, introduction to coroutines talk covers this topic

https://www.youtube.com/watch?v=_hfBv0a09Jc

k

kenkyee

12/12/2018, 3:33 AM
They conceptually all do the same thing...make more efficient use of a single thread or threads than regular threading by suspending code that has to wait on that thread...
g

gildor

12/12/2018, 3:45 AM
But recommended way to use coroutines in Kotlin is different, there is a concept of suspend function and async/await used only for explicitly asynchronous code. This difference covered in this talk and official guide
r

Rohan Maity

12/12/2018, 5:30 AM
Thank you ! I watched the intro video to coroutines What I Inferred from video is Async/await in c# are keywords , where async/await are library functions Now 1)how do this thing give more flexibility? 2)is just about the flexibility (if only comparing async/await in c# with async{} in Kotlin) or is there any other upper hand ? Also one thing I can think of is async{} starts coroutines . And Coroutines are lighter than Threads . So does this thing make async{} better than async/await in C#? Correct me if I am wrong please
g

gildor

12/12/2018, 5:33 AM
Kotlin coroutines is not only async/await, and it’s main point of Kotlin implementation, because language has only support of
suspend
functions and very basic builders. no async/awiat in the language. Even now in Kotlin on base of this implemented not only async/await, but also generators, Go-style channels (which also based on coroutines, but part of the language), actors and so on So this is definitely more flexible. So async/await implementation in Kotlin and C# is not so different in terms of use cases, but it’s just a small part of kotlinx.coroutines and actually not even part of language or stdlib Also default style recommended for kotlin is not async/await, but usage of sequential suspend functions
One more difference about async/await, that in Kotlin all coroutines builders use structured concurrency pattern, so always have parent that control their lifecycle and prevent async tasks leaking
async{} starts coroutines . And Coroutines are lighter than Threads . So does this thing make async{} better than async/await in C#?
I didn’t get your question %)
r

Rohan Maity

12/12/2018, 6:34 AM
@gildor thanks for such good answer. I got it Suspending functions are flexible. And async{} is also built on the top of suspending function. Coroutines are great. I also tried async/await in c# . To check out the difference. For use case and in working they do not seem different . Only difference I found out was that of one in c# are keywords and one in Kotlin are functions (on the top of suspending) But in the talk . It is said Kotlin async{} (only async{} not Coroutines as whole) provides better flexibility than async/await in other languages as in Kotlin async{} are functions whereas in other languages these are keywords If you don't mind can you give a simple example where async{}(only async{} not Coroutines as whole) being flexible than async/await
g

gildor

12/12/2018, 6:46 AM
If you don’t mind can you give a simple example where async{}(only async{} not Coroutines as whole) being flexible than async/await (edited)
For example, recently async and other coroutine builders (launch, actor etc) reimplemented on top of StructuredConcurrency, new approach where you always has parent of any async operations and cannot just run standalone coroutine with no lifecycle handling (you can, but you should do this explicitly in GlobalScope). More info https://medium.com/@elizarov/structured-concurrency-722d765aa952 If async/await would be just keywords it wouldn’t be possible to change, but because async is just a library function (same as Job, Dispatchers etc) you can change implementation Also async/await in C# always works with Task which is promise like abstraction of C#, but in Kotlin Coroutines you can write await implementation for any promise-like abstraction and just call it
myCustomFuture.await()
, which is also very powerful and flexible way which would be possible with keyword (or must provide some built-in API for integration which can have some limitations)
☝️ 1
One more difference, in C# you mark your function with
async
, but in Kotlin it’s not recommended, you rather wrap to
async
builder on call site to be explicit about concurrency and in Kotlin instead of use async on function declaration and return Deferred<T> you will just return T and mark function as suspend
r

Rohan Maity

12/12/2018, 6:54 AM
@gildor I read the article . Since async{} are functions so they can be supplied with scope . If scope runs out coutines will stop. But this is not possible with async/await keywords method (lifecycle handling is not possible) Is that so ? Also very much thanks to point out , that we can change implementation of await. This was I was looking for. To change the implementation I have to supply my version in onAwait?
g

gildor

12/12/2018, 6:57 AM
But this is not possible with async/await keywords method (lifecycle handling is not possible)
You of course can include this to language, if you want, but usually it’s much harder and you cannot break backward compatibility (which actually happen). And StructuredConcurreny not a part of C# or Go (but was originally proposed for Go) and now to add it it requires significant language changes which not a problem in case of Kotlin
To change the implementation I have to supply my version in onAwait? (edited)
What do you mean? `async{}`is just a coroutine builders that provide own promise-like abstraction. If you already have some, you can implement
await()
function for it, you don’t need
async
or
Deferred
r

Rohan Maity

12/12/2018, 7:05 AM
Ok . So If want the await implementation different . I have to implement my own CoroutineBuilder .
g

gildor

12/12/2018, 7:23 AM
no, you don’t need coroutine builder for existing promise/future abstraction
See for example integration with Guava LisenableFuture https://github.com/Kotlin/kotlinx.coroutines/tree/master/integration/kotlinx-coroutines-guava or with Java 8 CompletableFuture
But you can have coroutine builder that actually creates coroutine that converted to some other Promise. it can be useful as adapter for Java code which cannot use suspend functions directly easily see CompletableFuture builder: https://github.com/Kotlin/kotlinx.coroutines/tree/master/integration/kotlinx-coroutines-jdk8
r

Rohan Maity

12/12/2018, 7:48 AM
Thanks @gildor not only I cleared my doubt . I also got to know some more things
👍 1
10 Views