Last active
January 26, 2022 11:20
-
-
Save dalinaum/2fe826d5147b11b5b3684c6392bc4b55 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
fun getProfilePage() { | |
getUserById("fastcampus") | |
.map { getAdditionalUserInfo(e) } | |
// 내용이 길어질 수록 길어지는 체이닝 .XX, .YY. | |
// 줄줄이 쓰고 싶은데 어떤 것을 .map이나 .filter나 .flatMap에 나누어서 전달해야. | |
.subscribeWith(object: DisposableObserver<String>() { | |
override fun onError(e: Exception) { | |
handleUserInfo(e) | |
} | |
override fun onSuccess(additionalUserInfo: AdditionalUserInfo) { | |
handleUserInfo(additionalUserInfo) | |
} | |
}) | |
} |
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
suspend fun getProfilePage() { | |
val user = getUserById("fastcampus") | |
val additionalUserInfo = getAdditionalUserInfo(user) | |
handleUserInfo(additionalUserInfo) | |
// 직설적인 코드 (Straightforward) | |
// 에러는 외부의 CEH(Coroutine Exception Handler)에서 | |
} |
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
fun getProfilePage() { | |
getUserById("fastcampus", { e -> | |
handleException(e) | |
}, { user -> | |
getAdditionalUserInfo(user, { e -> | |
handleException(e) | |
}, { additionalUserInfo -> | |
handleUserInfo(additionalUserInfo) | |
// 점점 깊어지는 콜백 2개의 비동기 호출만 했을 뿐인데 이 정도 깊이. | |
// 5번 호출하면 어떻게 될까요? | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment