title: Callback get response Sync kotlin
body: <p>I'm tring to get the response of a callback in sync mode because I the value of the response is needed to all application to work, without this value ( token ) I can't continue to use the application.
  This is my companion object inside the retrofit interface. I need to set the token before creation of a client.</p>
  
  <p>What I'm doing wrong?</p>
  
  <p><strong>EDIT :</strong> </p>
  
  <p>I put as this Logs as you write :</p>
  
  <pre><code>companion object {
  
      private var mToken: String = ""
  
      fun create(activity: Activity): MyPhoneInterface {
          Log.d("tokenMyPhoneInterface", activity.localClassName)
          getToken(activity)
          Log.d("tokenMyPhoneInterface", "client token $mToken")
  
          val client = OkHttpClient.Builder()
                  .addInterceptor { chain -&gt;
                      val request = chain.request().newBuilder()
                              .addHeader("Authorization", mToken).build()
                      chain.proceed(request)
                  }.build()
  
          val retrofit = Retrofit.Builder()
                  .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                  .addConverterFactory(GsonConverterFactory.create())
                  .client(client)
                  .baseUrl(BuildConfig.API_HOST)
                  .build()
  
          return retrofit.create(MyPhoneInterface::class.java)
      }
  
      private fun getToken(activity: Activity) {
          if(!activity.isFinishing &amp;&amp; isJwtExpired(mToken)){
              val latch = CountDownLatch(1)
              (LoginManager(activity)).getToken(true, object : ServiceCallback&lt;String&gt; {
                  override fun success(token: String) {
                      Log.d("tokenMyPhoneInterface", "token $token")
                      mToken = token
                      latch.countDown()
                  }
                  override fun failure(serviceError: ServiceError) {
                      Log.d("tokenMyPhoneInterface", serviceError.errorMessage)
                      latch.countDown()
                  }
              })
              Log.d("tokenMyPhoneInterface", "before await ")
              latch.await()
              Log.d("tokenMyPhoneInterface", "after await")
          }
      }
  }
  </code></pre>
  
  <p>But I the system is blocked in the latch.await() and the logs is :</p>
  
  <p>05-14 18:19:00.127 848-848/com.italy.myphone D/tokenMyPhoneInterface: view.splash.activity.Splash
  05-14 18:19:00.131 848-848/com.italy.myphone D/tokenMyPhoneInterface: before await</p>
  
  <p><strong>EDIT answer2:</strong></p>
  
  <pre><code>sealed class TokenResult {
  class Success(val token: String) : TokenResult()
  class Error(val serviceError: ServiceError) : TokenResult()}
  
  
  
  suspend fun getToken(activity: Activity): TokenResult {
  return suspendCancellableCoroutine { continuation -&gt;
      (LoginManager(activity)).getToken(true, object : ServiceCallback&lt;String&gt; {
          override fun success(token: String) {
              continuation.resume(TokenResult.Success(token))
          }
  
          override fun failure(serviceError: ServiceError) {
              continuation.resume(TokenResult.Error(serviceError))
          }
      })
  }}
  </code></pre>
  
  <p>And this is how to I try to call the suspend function :</p>
  
  <pre><code>companion object {
      fun create(activity: Activity): MyPhoneInterface{
          Log.d("tokenMyPhoneInterface", activity.localClassName)
  
  
          var token = runBlocking {
              return@runBlocking getToken(activity)
          }
  
  
          Log.d("tokenMyPhoneInterface", "obtained token")
  
          Log.d("tokenMyPhoneInterface", "client token $tokenResult")
  
          val client = OkHttpClient.Builder()
                  .addInterceptor { chain -&gt;
                      val request = chain.request().newBuilder()
                              .addHeader("Authorization", "").build()
                      chain.proceed(request)
                  }.build()
  
          val retrofit = Retrofit.Builder()
                  .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                  .addConverterFactory(GsonConverterFactory.create())
                  .client(client)
                  .baseUrl(BuildConfig.API_HOST)
                  .build()
  
          return retrofit.create(MyPhoneInterface::class.java)
      }
  }
  </code></pre>
  
  <p>That is inside an interface and this is the code that I use to call the interface/companion object in the activity :</p>
  
  <pre><code>    private val mMyPhoneInterface by lazy {
      MyPhoneInterface.create(activity)
  }
  </code></pre>