Predict the output
What does this print?
enum E: Error { case bad }
[1, 2, 3].publisher
.tryMap { v -> Int in
if v == 2 { throw E.bad }
return v
}
.replaceError(with: 0)
.sink { print($0) }Hint 1 of 1
What happens to a publisher after it fails?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: B 1, 0
Correct. replaceError emits 0 and then the pipeline finishes.
Why not the others?
- A 1, 0, 3. After the error the upstream has finished, so 3 is never sent.
- C 1, 3. replaceError emits its replacement value.
- D 1. replaceError emits 0 before finishing.