# Use Retrofit asynchronously or use Coroutines for data fetching from server

**URL:** <https://discuss.kotlinlang.org/t/use-retrofit-asynchronously-or-use-coroutines-for-data-fetching-from-server/25773>\
**Category:** Uncategorized\
**Created:** [October 10, 2022, 8:02am UTC](https://discuss.kotlinlang.org/t/use-retrofit-asynchronously-or-use-coroutines-for-data-fetching-from-server/25773 "2022-10-10T08:02:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibrahimgiki](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ibrahimgiki/32/9360_2.png) [@ibrahimgiki](https://discuss.kotlinlang.org/u/ibrahimgiki)\
**Post date:** [October 10, 2022, 8:02am UTC](https://discuss.kotlinlang.org/t/use-retrofit-asynchronously-or-use-coroutines-for-data-fetching-from-server/25773/1 "2022-10-10T08:02:49Z")

</div>

I am making a server request to get some data (asynchronously) using Retrofit lib. I have two options

1. To use Retrofit lib asynchronously by overriding _ **onResponse** _ and _ **onFailure** _ methods as shown below.

> var call : Call = RetrofitClientNew.apiInterface.getTehsilNamesGET(districtID.toString())
> 
> ```
> call.enqueue(object : Callback<Tehsil1> {
> override fun onResponse(call: Call<Tehsil1>, response: Response<Tehsil1>) {
> TODO("Not yet implemented")
> }
> 
> override fun onFailure(call: Call<Tehsil1>, t: Throwable) {
> TODO("Not yet implemented")
> }
> })
> 
> ```

1. Use _Coroutines_ (for asynchronous programming) and return JsonObject from Retrofit without overriding _ **onResponse** _ and _ **onFailure** _ methods as shown below.

> withContext([Dispatchers.IO](http://Dispatchers.IO)) {
> 
> ```
> val response : Response<JsonObject?>? = RetrofitClientNew.apiInterface.getTehsilNames(districtID.toString())
> 
> if (response!!.isSuccessful) {
> 
> }
> }
> 
> ```

Which approach is better ? Thank you

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [October 10, 2022, 9:06am UTC](https://discuss.kotlinlang.org/t/use-retrofit-asynchronously-or-use-coroutines-for-data-fetching-from-server/25773/2 "2022-10-10T09:06:57Z")

</div>

Whatever suits you best 🙂 Assuming that in the second example `getTehsilNames()` is a suspend function, both these code samples should internally work in a similar way. Sequential code (suspend function) is usually considered to be much easier to handle than callbacks-based code, but on the other hand it requires you to learn and understand coroutines. If this is the only place where you would use coroutines then maybe it is better to go with callbacks, but if you use coroutines already, I suggest going suspend.

---

<div class="post-metadata">

**Author:** ![ibrahimgiki](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ibrahimgiki/32/9360_2.png) [@ibrahimgiki](https://discuss.kotlinlang.org/u/ibrahimgiki)\
**Post date:** [October 10, 2022, 9:10am UTC](https://discuss.kotlinlang.org/t/use-retrofit-asynchronously-or-use-coroutines-for-data-fetching-from-server/25773/3 "2022-10-10T09:10:48Z")

</div>

Thank you for your kind suggestion.

---

<div class="post-metadata">

**Author:** ![tamegajr](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@tamegajr](https://discuss.kotlinlang.org/u/tamegajr)\
**Post date:** [October 18, 2022, 3:16pm UTC](https://discuss.kotlinlang.org/t/use-retrofit-asynchronously-or-use-coroutines-for-data-fetching-from-server/25773/4 "2022-10-18T15:16:32Z")

</div>

I agree with broot, the best solution is the one that suits you better. I have some expierence with Coroutines because I’m using Firestore Database, and all documentation associated uses Coroutines to deal with Firestore. And in my opinion, the only drawback I had was to understand how to debug a Coroutine, but after understanding Coroutines way of work, it became pretty obvious how to debug it.
