Rohan Maity
12/12/2018, 1:03 AMserebit
12/12/2018, 1:06 AMasync { suspendingFunction() }.await()
gildor
12/12/2018, 1:39 AMkenkyee
12/12/2018, 3:33 AMgildor
12/12/2018, 3:45 AMRohan Maity
12/12/2018, 5:30 AMgildor
12/12/2018, 5:33 AMsuspend
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 functionsasync{} 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 %)
Rohan Maity
12/12/2018, 6:34 AMgildor
12/12/2018, 6:46 AMIf 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)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 suspendRohan Maity
12/12/2018, 6:54 AMgildor
12/12/2018, 6:57 AMBut 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
Rohan Maity
12/12/2018, 7:05 AMgildor
12/12/2018, 7:23 AMRohan Maity
12/12/2018, 7:48 AM