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

Animesh Sahu

08/07/2019, 3:35 AM
Is there another use of suspend functions other than delay()? I don't get it which function can be used in suspend to actually suspend it. For example if i call any api or something it will definitely block the coroutine right? So how should i use coroutine there so i can do something till the response is recieved from the server?
g

gildor

08/07/2019, 3:39 AM
don’t get it which function can be used in suspend to actually suspend it
Any asynchronous function with coroutine adapter
For example if i call any api or something it will definitely block the coroutine right
No, if this API is Asyncronous (it just use callbacks/promises/features/etc)
So how should i use coroutine there so i can do something till the response is recieved from the server
Depends on what kind API you use for this now
a

Animesh Sahu

08/07/2019, 3:56 AM
@gildor how do you create a suspendable api? Any example of suspendable apis around internet so i can understand properly?
g

gildor

08/07/2019, 3:59 AM
Kotlinx.coroutines has adapters for different async API like RxJava, CompletableFuture etc You can check implementation What do you use now?
a

Animesh Sahu

08/07/2019, 4:06 AM
@gildor adapters for sockets, http, etc. Kinda confused.
g

gildor

08/07/2019, 4:22 AM
Http depends on your http client
Again, what API exactly do you use now?
u

uli

08/07/2019, 5:09 AM
s

SiebelsTim

08/07/2019, 6:24 AM
You basically just block a different thread from the thread you are on to free your thread.
u

uli

08/07/2019, 6:35 AM
It depends, if you talk about io for example, nio offers real asynchronous, callback based apis. But sure, if your workload is bocking, there is nothing coroutines can do about it but move the load to a different thread.
Async io as in java nio is directly based on interrupts and posix signals and thereby really does not block any thread
5 Views