Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.shopify.coroutine.sample
- import kotlinx.coroutines.delay
- import kotlinx.coroutines.isActive
- import kotlinx.coroutines.runBlocking
- import kotlin.coroutines.coroutineContext
- fun main(vararg args: String) {
- // suspendFunc1() //- won't compile outside coroutine or another suspend function
- // coroutine builder
- runBlocking {
- suspendFunc1() // call suspend function from coroutine
- }
- println("all done")
- }
- private suspend fun suspendFunc1() {
- if (coroutineContext.isActive) {
- println("coroutine is active")
- }
- suspendFunc2() // call another suspend functions
- regularFunc() // call regular functions
- println("suspend function 1 - done")
- }
- private suspend fun suspendFunc2() {
- delay(500) // stdlib coroutine suspend function
- println("suspend function 2 - done")
- }
- private fun regularFunc() {
- println("regular - done")
- }
Add Comment
Please, Sign In to add comment