Skip to content

Instantly share code, notes, and snippets.

@devrath
Created October 9, 2021 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devrath/7da194fb8544e08ea42ef6653e9074ba to your computer and use it in GitHub Desktop.
Save devrath/7da194fb8544e08ea42ef6653e9074ba to your computer and use it in GitHub Desktop.
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