Pick the fix
Callers sometimes wait forever. Which change removes this whole class of bug?
func loadUser(id: String, completion: @escaping (User?) -> Void) {
guard !id.isEmpty else { return }
api.fetchUser(id) { user in
completion(user)
}
}Hint 1 of 2
Look at the guard line: what does the caller get?
Hint 2 of 2
Which style lets the compiler check every exit path?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Make it func loadUser(id: String) async throws -> User and throw on the empty id
Correct. Every path must now return or throw, and the compiler enforces it.
Why not the others?
- B Add
completion(nil)inside the guard. That fixes this one path, but the next early return can repeat the bug; nothing checks it. - C Call
completioninsideDispatchQueue.main.async. That changes which thread the callback runs on, not whether it runs. - D Mark the closure
@Sendable. That's about thread safety of captured values, not about the callback being called.