0% found this document useful (0 votes)
25 views7 pages

5 Advantages of Async-Await in Swift

Uploaded by

wiki vik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

5 Advantages of Async-Await in Swift

Uploaded by

wiki vik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

5 advantages of

async-await in Swift
The end of completion handlers ????
"async-await" is a programming feature in
swift, used particularly when dealing with
tasks that might take some time to
complete, such as network requests, file I/O,
or database operations. It aims to make
asynchronous code more readable and
maintainable by reducing the complexity
associated with nested completion
handlers or callbacks.

async await is part of the new


structured concurrency changes
that arrived in Swift 5.5 during
WWDC 2021.
Syntax
/*
*****Async function******
*/

func fetchUsers() async throws -> [UIImage] {


// .. perform data request
}

/*
*****Syntax to call async function*****
*/
do {
let users = try await fetchUsers()
print("Fetched \(users.count) users.")
} catch {
print("Fetching users failed with error \(error)")
}
Advantages
Readability and Maintainability
The async-await version is more linear and easier to read. There's no need to
manage nested closures, making the code more maintainable.

Synchronous-Like Flow
The async-await version looks similar to synchronous code, making it easier
to understand the sequence of operations.

Error Handling
In the async-await version, errors are propagated using 'throws', and you can
use 'try-catch' for error handling. This is more intuitive and clean compared
to handling errors through completion handlers.

Scope and Variables


Variables are captured automatically without the need for explicit capture
lists. This reduces the chance of memory leaks and simplifies variable
management.

Debugging
Debugging is simplified as the control flow resembles synchronous code.
You can set breakpoints and inspect variables more easily.
One More Thing!!
async-await is just the foundation of new
concurrency model introduced in swift
recently. There are newer techniques to do
tasks asynchronously. Details and more on
this coming soon.
Follow me
🙏🏼
More on this
coming soon!!
Launch
Announcement
Launching soon
"The Knock-outs".
An interview styled discussion
on very important iOS and
other engineering
fundamentals. Don't miss it.
Follow me 🙏🏼 to stay updated.

You might also like