having problems understanding these function param...
# android
p
having problems understanding these function params. i have very little knowledge about callback fun. can someone explain how these params are working?
Copy code
fun loadBlogArticles(onSuccess: (List<Blog>) -> Unit, onError: () -> Unit) {
    val request = Request.Builder()
            .get()
            .url(BLOG_ARTICLES_URL)
            .build()

    executor.execute {
        runCatching {
            val response: Response = client.newCall(request).execute()
            response.body?.string()?.let { json ->
                gson.fromJson(json, BlogData::class.java)?.let { blogData ->
                    return@runCatching blogData.data
                }
            }
        }.onFailure { e: Throwable ->
            Log.e("BlogHttpClient", "Error loading blog articles", e)
            onError()
        }.onSuccess { value: List<Blog>? ->
            onSuccess(value ?: emptyList())
        }
    }
}
r
onSuccess is a function that takes in a List<Blog> an returns a Unit
❤️ 1
e
onError is also a function, does not take in any argument but also returns a Unit. This means you can call these functions and do whatever you want when the call is successful in onSuccess with the List<Blog> object and when there's an error, you can as well do what you want in the onError function
🙏 1