Created
October 9, 2021 17:43
-
-
Save devrath/7da194fb8544e08ea42ef6653e9074ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LegacyCallbackSampleTwo( | |
override val coroutineContext: CoroutineContext | |
) : CoroutineScope { | |
fun initiateDemo() { | |
launch { | |
val user = getUser("111") | |
println(user.userName) | |
} | |
} | |
private suspend fun getUser(id: String): User = suspendCoroutine { | |
try { | |
val result = MyTask().execute(id).get() | |
it.resume(result) | |
} catch (e: Exception) { | |
e.printStackTrace() | |
println(e.message) | |
} | |
} | |
class MyTask : AsyncTask<String?, Void?, User>() { | |
protected override fun doInBackground(vararg userId: String?): User? { | |
try { | |
return User(userId.toString(), "Async User") | |
} catch (e: MalformedURLException) { | |
e.printStackTrace() | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
return null | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment