This content originally appeared on Level Up Coding - Medium and was authored by Aaina jain
It may not completely replace but soon you will have less usage of Result type in codebase
Earlier we used to write 2 optional values in a closure, on the consumer side need to check which one has value and then process using if else condition.
func fetchContent(completion: ([Model]?, Error?) -> Void) {
// .. perform data request }
With Result type in place this boilerplate got reduced and we could elegantly handle success and failure. Still code looks messy when we have another pending request based on current api response. As the calls get nested and nested….., it can become a problem when it comes to readability. You can take action and move every “pyramid floor” into its own function, but that’s more of a patch. Till now we talked about callback based concurrency, let’s see how can we use Async/await api for concurrency which is supported from iOS 13.
Async/await allows us to write asynchronous code just as we write regular code.
Async/await facts:
- async enables a function to suspend
- await marks where an async function may suspend execution, other work can happen during a suspension
- Once an awaited async call completes, execution resumes after the await.
When async/await is more useful?:
Let’s say you have a complex asynchronous block of code and on success or failure you have to call completion block. In that situation async/await can simplify code and there will be less chances of forgetting to call completion block.
How to incorporate async/await in existing closure codebase using Result Type??
func fetch<T: Codable>(request: APIData, basePath: String, completionHandler: @escaping ((Result<T, Error>) -> Void)) {}
can become
Here we just wrapped existing closure in withCheckedThrowingContinuation to return T. There are multiples variants of resume
Condition of using Continuations is Continuation must be resumed exactly once on every path.
public func resume<Er>(with result: Result<T, Er>) where E == Error, Er : Error
public func resume(returning x: T)
public func resume(throwing x: E)
If you just want to handle happy case then you can wrap closure in withCheckedContinuation as well.
On consumer side we can have something like this:
If we have nested dependent apis then also code will look simpler.
Now let’s look into how to call this function from Controller/View Model
A Task allows us to create a concurrent environment from a non-concurrent method, calling methods using async/await.
Some useful articles if you wanna go into details:
Async await in Swift explained with code examples
https://developer.apple.com/videos/play/wwdc2021/10132/
Modern Concurrency in Swift: Introduction
You can reach out to me at:
Linkedin: Aaina Jain
Twitter: __aainajain
Will async/await replace Result type 🤔 was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Aaina jain
Aaina jain | Sciencx (2023-02-17T15:50:40+00:00) Will async/await replace Result type. Retrieved from https://www.scien.cx/2023/02/17/will-async-await-replace-result-type/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.