Skip to content

Instantly share code, notes, and snippets.

@d-plaindoux
Last active May 1, 2021 08:45
Show Gist options
  • Save d-plaindoux/9005171dccd1ffa83594cb13ed5380b0 to your computer and use it in GitHub Desktop.
Save d-plaindoux/9005171dccd1ffa83594cb13ed5380b0 to your computer and use it in GitHub Desktop.
Naive example composing functions using the network, http web client and files.
suspend fun doSomething(webView: WebView): Int {
val v1 = firstAction()
val v2 = secondAction(webView)
val v3 = thirdAction()
return v1 + v2 + v3 // .i.e. 42
}
suspend fun firstAction(): Int =
// Do some stuff on the network for example ...
10
suspend fun secondAction(webView: WebView): Int =
suspendCoroutine { cont ->
webView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
cont.resume(30)
}
}
webView.loadUrl(AnURL)
}
suspend fun thirdAction(): Int =
// Do some stuff using file for instance ...
2
@d-plaindoux
Copy link
Author

This last version uses Kotlin idiomatic suspended coroutines (No more Arrow.Kt)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment