19 of 22 · Callbacks and concurrency
Promises → async/await
Futures and promises turn "call me back" into a value that represents a result arriving later. Swift's async/await makes that part of the language: an async function suspends, then returns or throws exactly once.
How it's wired
- Combine's Future is a one-shot promise. async functions return or throw exactly once. withCheckedThrowingContinuation bridges old callbacks.
| Part | Owns | Looks like |
|---|---|---|
| async function | Returns or throws exactly once | func send() async throws |
| Continuation | Wrap a callback API | withCheckedThrowingContinuation |
| Future | One-shot value object (Combine) | Future<Output, Failure> |
What staff engineers watch for
- Bridging: wrap callback APIs with withCheckedThrowingContinuation. It traps on a double resume and logs if never resumed. Unsafe continuations remove those checks.
- Cancellation is cooperative. Cancelling a Task doesn't stop the callback API underneath; use withTaskCancellationHandler to cancel the real work.
- Task {} is unstructured: no automatic cancellation, and it inherits the current actor. Prefer async let and task groups, and .task in SwiftUI so work dies with the view.
- Actor reentrancy: across an await inside an actor, state can change. Re-check invariants after every suspension point.
What I'd use
| New one-shot async work | async/await. |
|---|---|
| Streams of values | AsyncSequence, or Combine where you need operators. |
| Legacy callback APIs | Wrap once in a checked continuation. |
Compared
Future vs async/await vs AsyncSequence
| Future | async/await | AsyncSequence | |
|---|---|---|---|
| Values | One | One | Many |
| Cancellation | Cancel subscription | Task cancellation | Cancel the consuming task |
| Errors | Failure type | throws | throws per element |
Expert notes
- Structured concurrency is the point: work that can't outlive its scope can't leak.
Check yourself
Answer out loud before you reveal. Recall is what makes it stick.
Explain Promises → async/await in 30 seconds.
- Futures turned a callback into a value, and async/await made it part of the language: an async function suspends, then returns or throws exactly once.
- I bridge legacy callback APIs once with withCheckedThrowingContinuation, so a double resume traps in development instead of silently duplicating work.
- The things to watch are cooperative cancellation and unstructured Task {} work that outlives its screen.
How do you wrap a completion-handler API with async/await, and which rule must never be broken?
- Use
withCheckedThrowingContinuation, call the old API inside, and resume from its callback. - The rule is that every path resumes exactly once.
- A checked continuation traps on a double resume in every build and logs a warning if it's dropped.
- The unsafe version turns a double resume into undefined behavior and a dropped one into a silently leaked task.
Profiling shows checked continuations in a hot path. Can you switch to unsafe ones, and what do you give up?
- Yes, the APIs are identical, so it's a one-word swap.
- Only do it after the wrapper is verified and profiling proves it matters.
- You lose the safety net.
- A double resume becomes undefined behavior instead of a trap, and a dropped continuation silently leaks the task.
- Make sure tests cover every callback path before you swap.
A UIKit button handler must call an async method and show errors. How do you bridge sync and async?
- Start a
Taskin the handler:Task { do { try await save() } catch { show(error) } }. - Inside, you get normal do/try/catch and main-actor UI updates.
- The catch: the
Taskkeepsselfalive until it finishes. - That's fine for a short save, but for long work keep a handle and cancel it when the screen goes away.
- Wrap old callback APIs once with
withCheckedThrowingContinuation.