Reusing blocks of code
Let's say you have a method which performs some networking actions, which can obviously fail, but in different ways:
func performNetworkStuff() {
fetchSomething(then: { condition in
if condition {
}
// failure due to condition
else {
failureAction1()
failureAction2()
variable1 = value
etc...
}
}) {
// failure due to network
failureAction1()
failureAction2()
variable1 = value
etc...
}
}But instead, we could DRY it up a little bit, by storing the failure actions into a block beforehand: