Reputation: 127
In the example below (from Kotlin docs), there are nested callback functions.
Assuming postItem
is called on the main thread, is it true that immediately after calling preparePostAsync
the main thread continues it's execution without waiting for preparePostAsync
to finish?
If yes, does this mean that Kotlin is actually performing multi-threading under the covers of callback functions? (Assuming this is true, would it mean that the number of callbacks = number of threads?)
fun postItem(item: Item) {
preparePostAsync { token ->
submitPostAsync(token, item) { post ->
processPost(post)
}
}
}
If it is not related to multi-threading, then what is the difference between callback functions and multi-threading?
Upvotes: 1
Views: 450
Reputation: 93609
It depends entirely on the API you're working with. Those examples in the docs are imaginary ones, not part of the standard library.
Typically, when a function takes a callback, it returns immediately after enqueuing the request, and will call the callback on the main thread when a result comes back.
Network libraries like OkHttp use a thread pool under the hood, so there is not necessarily a one-to-one correspondence of running background threads to number of requests made.
The word "callback" is just an informal name for asynchronous tasks that follow this pattern.
Upvotes: 1