Can someone point me at some sort of article/post ...
# announcements
d
Can someone point me at some sort of article/post which give high level overview of lower level implementation of asynchronous apis please? Like how did callbacks help with async in javascript, what sets actual values in futures/promises, what makes kotlin suspended function resume, does something poll state of suspended function etc etc?
l
This talk is a great resource to know the internals:

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

There has been a little update since then as there's
resume
and
resumeWithException
are now extensions delegating to
resumeWith
.
👍 1
d
does it cover theory in general, not just specific kotlin stuff?
g
It's specific Kotlin stuff, and even more specific to coroutines
how did callback help with async in javascript
Because you cannot use asyncronous APIs without callbacks (or any other callback based abstraction like promise/future/coroutine)
Nothing polls state of suspended function directly, same for future/promise, simplest explanation that some other code calls callback/future/promise/coroutine resume. This code is some other thread, code, runtime, system event. Under the hood of all those events are poll loops, but it depends how deep it is, sometimes on library level, sometimes on runtime level, sometimes on system/os and on most low level CPU clock. Particular implementation just depends on nature of this async code
In case of JavaScript it's JS runtime dispatches network request, time intervals and expose this API as callbacks or promises
d
I didn't realize everything is based on callbacks
does that low level system machinery (sockets etc) expose APIs to register a callback with, which then puts an event on the event loop?
g
Yes, Promise/Future is just an abstraction for callbacks which allows to implement additional operators and simplify composition, but it's just a wrapper for callback Suspend function is also callback, but hidden under language feature, which compiled to code that quite similar to callbacks (or to futures/promises in another languages)
https://kotlinlang.slack.com/archives/C0922A726/p1565959076455900?thread_ts=1565946172.448600&cid=C0922A726 It depends, sometimes it provides callbacks, sometimes it's just polling for some resource, depends on implementation details and level of abstraction. For example you can use Java NIO, which uses system dependant APIs to provide real asyncronous IO, but you also can use standard io which is blocking, do not have any callbacks, so to use it you invoke, it blocks thread, but you can wrap it to some asyncronous API which under the hood starts thread and when operation is finished notifies callbacks
d
I was thinking even lower level, like how does Java NIO do it underneath
g
It depends on operation system, it just abstracts those APIs
On Linux for example it most probably uses epoll, just Google it if you interesting about particular implementation details
d
thank you! that all makes sense now
👍 1