# Converting method with call backs to coroutine

**URL:** <https://discuss.kotlinlang.org/t/converting-method-with-call-backs-to-coroutine/12136>\
**Category:** Libraries\
**Created:** [March 28, 2019, 10:30am UTC](https://discuss.kotlinlang.org/t/converting-method-with-call-backs-to-coroutine/12136 "2019-03-28T10:30:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jnkbn](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@jnkbn](https://discuss.kotlinlang.org/u/jnkbn)\
**Post date:** [March 28, 2019, 10:30am UTC](https://discuss.kotlinlang.org/t/converting-method-with-call-backs-to-coroutine/12136/1 "2019-03-28T10:30:54Z")

</div>

Hi everyone, I have recently started to learn Kotlin and need help with converting a function with callbacks to coroutine.

I have converted my function as below.

```
private suspend inline fun tryGettingTokenSilently(account: IAccount?, authenticationManager: AuthenticationManager): OneDriveAuth = suspendCoroutine { continuation ->
    authenticationManager.callAcquireTokenSilent(account, true, object : MSALAuthenticationCallback {
        override fun onSuccess(authenticationResult: AuthenticationResult) {
            continuation.resume(OneDriveAuth.Success(authenticationResult.account.username, authenticationResult.accessToken))
        }

        override fun onError(exception: MsalException) {
            when (exception) {
                is MsalClientException ->
                    continuation.resume(OneDriveAuth.Fail(exception))
                is MsalServiceException ->
                    continuation.resume(OneDriveAuth.Fail(exception))
                is MsalUiRequiredException -> {
                    continuation.resume(OneDriveAuth.RequireLogin)
                }
            }
        }

        override fun onError(exception: Exception) {
            continuation.resume(OneDriveAuth.Fail(exception))
        }

        override fun onCancel() {
            continuation.resume(OneDriveAuth.UserCancelled)
        }
    })
}

```

I am having hard time calling it from my utility class. For example, I’ve tried with

```
runBlocking {
            if (Logger.INSTANCE.DEBUG) {
                Logger.INSTANCE.log(LOG_TAG, "getTokenOrRequestLogin runBlocking")
            }
            tryGettingTokenSilently(account, authenticationManager)
        }

```

Which blocks infinitely, it looks like continuation.resume(s) never called and it just hangs there.

I know I can do the following in my activity/fragment

```
 CoroutineScope(Dispatchers.Main).launch{
        val result = tryGettingTokenSilently(....)
  }

```

But, this returns Job and prevents me getting the return from the method. I want to return the value to another method.What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [April 4, 2019, 7:25pm UTC](https://discuss.kotlinlang.org/t/converting-method-with-call-backs-to-coroutine/12136/2 "2019-04-04T19:25:49Z")

</div>

> [@jnkbn](#):
>
> Which blocks infinitely, it looks like continuation.resume(s) never called and it just hangs there.

Yes, I agree.  
Try to debug or putting some logging on each callback.  
Try to replace:

```auto
        override fun onError(exception: MsalException) {
            when (exception) {
                is MsalUiRequiredException ->
                    continuation.resume(OneDriveAuth.RequireLogin)
                else ->
                    continuation.resume(OneDriveAuth.Fail(exception))
            }
        }

```
