Created
September 15, 2022 20:53
-
-
Save s1monw1/4b52c0beda704c16974ee301a4fff13e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Before Refactoring | |
val responseAsString = response.use { | |
val responseBytes = it.body()?.source()?.readByteArray() | |
if (responseBytes != null) { | |
String(responseBytes) | |
} else throw IllegalStateException("No response from server!") | |
} | |
// With Scope Function | |
val responseAsString = response.use { | |
it.body()?.source()?.readByteArray()?.let { String(it) } | |
?: throw IllegalStateException("No response from server!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment