Guest User

Untitled

a guest
Nov 13th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. package com.shopify.coroutine.sample
  2.  
  3. import kotlinx.coroutines.delay
  4. import kotlinx.coroutines.isActive
  5. import kotlinx.coroutines.runBlocking
  6. import kotlin.coroutines.coroutineContext
  7.  
  8. fun main(vararg args: String) {
  9. // suspendFunc1() //- won't compile outside coroutine or another suspend function
  10.  
  11. // coroutine builder
  12. runBlocking {
  13. suspendFunc1() // call suspend function from coroutine
  14. }
  15.  
  16. println("all done")
  17. }
  18.  
  19. private suspend fun suspendFunc1() {
  20. if (coroutineContext.isActive) {
  21. println("coroutine is active")
  22. }
  23.  
  24. suspendFunc2() // call another suspend functions
  25.  
  26. regularFunc() // call regular functions
  27.  
  28. println("suspend function 1 - done")
  29. }
  30.  
  31. private suspend fun suspendFunc2() {
  32. delay(500) // stdlib coroutine suspend function
  33. println("suspend function 2 - done")
  34. }
  35.  
  36. private fun regularFunc() {
  37. println("regular - done")
  38. }
Add Comment
Please, Sign In to add comment