Sequential operations

One of the most important benefits of the coroutines approach is a guarantee that functions are invoked in the same order in which they are written. The order of the operations is a very important nuance when we execute concurrent code in a multithreaded environment. 

Let's imagine that we have to load a user's details, using the following function:

suspend fun loadUserDetails(): User {
delay(3000)
return User(0, "avatar")
}

The loadUserDetails function invokes the delay function from the coroutines-core library and returns an instance of the User class. The delay function suspends the invocation of the current coroutine. When a user is ready, we have to pass a value of the avatar property to the loadImage function:

suspend fun loadImage(avatar: String): Image {
delay(3000)
return Image()
}

The loadImage function also invokes the delay function, and returns an instance of the Image class. We should then pass the received instance of the Image class to the showImage function.

The following code shows how to execute these functions sequentially, using coroutines:

fun main(args: Array<String>) = runBlocking {
val user = async { loadUserDetails() }.await()
val image = async { loadImage(user.avatar) }.await()
showImage(image)
}

The preceding snippet invokes all three functions that use different coroutines, sequentially. The following diagram shows a sequence of functions invoking:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.93.0