Is there another use of suspend functions other th...
# coroutines
a
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
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
@gildor how do you create a suspendable api? Any example of suspendable apis around internet so i can understand properly?
g
Kotlinx.coroutines has adapters for different async API like RxJava, CompletableFuture etc You can check implementation What do you use now?
a
@gildor adapters for sockets, http, etc. Kinda confused.
g
Http depends on your http client
Again, what API exactly do you use now?
u
s
You basically just block a different thread from the thread you are on to free your thread.
u
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