Objc PromiseKit Add new promises from within a promise
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Adding a new async operation inside a PromiseKit chain is standard practice in Objective-C, but it must be done with the correct return pattern. If a then block starts a new request and does not return it, sequencing and error propagation break. Clean promise composition keeps async flows predictable, testable, and easier to refactor.
Return the New Promise From then
Whenever the next step is asynchronous, return the promise from the then block.
PromiseKit flattens the returned promise, so downstream steps wait for completion automatically.
Anti-Pattern to Avoid
Incorrect pattern:
- start nested async call inside
then - do not return the nested promise
- outer chain continues early
That leads to race conditions and hidden failures. If you ever see nested callbacks inside then without a return statement, fix it immediately.
Conditional Async Branching
You can return different promises depending on the previous result.
All branches should return compatible value types for the next chain step.
Fan-Out and Join With PMKWhen
After one async step, you may need parallel calls and one join point.
This pattern removes manual counter logic and keeps failure behavior consistent.
Error Handling and Recovery
Use catch for terminal error handling. Use recover where a fallback is valid.
Do not hide errors with broad recovery unless the business case is explicit.
Lifetime and Capture Management
Long async chains can keep objects alive unintentionally. For UI controllers, weak capture can prevent retain cycles.
Use this carefully, since deallocated owners might mean updates should be skipped intentionally.
Test Promise Composition
Unit tests should verify:
- order of async steps
- branch behavior for stale and fresh cache paths
- rejection propagation from nested promises
- fallback behavior from
recover
Mock each dependency as a promise-producing method so you can test flow logic deterministically.
It also helps to log a lightweight chain identifier through each step so production traces show where a promise path diverged or failed.
For team code reviews, require every then block to show whether it returns a plain value or a promise, since that single distinction explains most sequencing defects.
Common Pitfalls
- Launching nested async work inside
thenand forgetting to return the new promise. - Mixing callback-style APIs and promises without adaptation boundaries.
- Returning inconsistent types from conditional branches in one chain.
- Swallowing critical errors with over-broad
recoverlogic. - Capturing controller instances strongly in long-lived chains.
Summary
- Promise chaining in Objective-C is reliable when every async step returns a promise.
- Conditional and parallel workflows are clean with returned promises and
PMKWhen. - Error handling should distinguish recovery paths from terminal failures.
- Capture strategy matters for UI lifetime and memory safety.
- Focus tests on sequence, branch correctness, and rejection propagation.

