Skip to content

Instantly share code, notes, and snippets.

@s1monw1
Created September 15, 2022 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s1monw1/4b52c0beda704c16974ee301a4fff13e to your computer and use it in GitHub Desktop.
Save s1monw1/4b52c0beda704c16974ee301a4fff13e to your computer and use it in GitHub Desktop.
// 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