Objc PromiseKit Add new promises from within a promise
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
Related reading
- Objective-C Asynchronous Web Request with Cookies
- Objects created in a thread can only be used in that same thread
- Observable createasync
- Offline data with replication/synchronization for Xamarin app on IPhone?
- Objective-C - Remove last character from string
- Objective-C and Swift URL encoding
- omp parallel vs. omp parallel for
- On what CPU cores are my Python processes running?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.