Last active
August 20, 2019 11:34
-
-
Save uzzu/5f834353804466ff0e42531990cf1332 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.ExperimentalCoroutinesApi | |
import kotlinx.coroutines.async | |
import kotlinx.coroutines.delay | |
import kotlinx.coroutines.runBlocking | |
class Hoge | |
class Fuga | |
@ExperimentalCoroutinesApi | |
suspend fun hoge(): Hoge { | |
println("hoge started") | |
delay(200) | |
println("hoge finished") | |
return Hoge() | |
} | |
@ExperimentalCoroutinesApi | |
suspend fun fuga(): Fuga { | |
println("fuga started") | |
delay(100) | |
println("fuga finished") | |
return Fuga() | |
} | |
fun mainNonAsync()= runBlocking<Unit> { | |
runCatching { | |
val h = hoge() | |
val f = fuga() | |
Pair(h, f) | |
} | |
.onSuccess { (h, f) -> | |
println("$h, $f") | |
} | |
} | |
fun mainAsync()= runBlocking<Unit> { | |
runCatching { | |
val h = async { hoge() } | |
val f = async { fuga() } | |
Pair(h.await(), f.await()) | |
} | |
.onSuccess { (h, f) -> | |
println("$h, $f") | |
} | |
} | |
// mainNonAsync() | |
mainAsync() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment