can suspend and resume its execution at certain locations. The code inside coroutine can be expressed sequentially. It's an old concept, implemented by many programming languages. Kotlin support coroutines since release 1.1 . Most of the coroutines related code that JetBrains wrote is part of their extensions libraries of kotlinx.coroutines . In Kotlin suspendable function mark with the suspend modifier, only coroutines can run suspend functions
is a hammer, everything looks like a nail fun login(credentials: Credentials): Single<UserID> fun loadUserData(userID: UserID): Single<UserData> fun showData(data: UserData) fun showUserInfo(credentials: Credentials) { login(credentials) .flatMap { loadUserData(it) } .doOnSuccess { showData(it) } .subscribe() } suspend fun login(credentials: Credentials): UserID suspend fun loadUserData(userID: UserID): UserData fun showData(data: UserData) suspend fun showUserInfo(credentials: Credentials) { val userID = login(credentials) val userData = loadUserData(userID) showData(userData) }
expressed sequentially Easy to write our own abstraction and adjust the use of it to our architecture Kotlinx.corout ines have out of the box integration with other major frameworks like RX 1 / 2, Java 8 stream and more Get ready for multi-platform world, coroutines might be the right way to write common asynchronous code