How to use async and await in Swift projects

Oscar de la Hera Gomez
Two flowers representing Swift and Xcode. Beneath them sits the text "Async Await."

To use async await in Swift, wrap the code in a Task. If the function throws an error, use do-try-catch syntax within the Task.

Basic use

An image showing a task implementing an asynchronous function that uses await code.

To use async-await syntax without error handling, wrap the code in a task, as demonstrated in the snippet below.

This calls the function shown in the do-try-catch syntax example offered below.

Do-Try-Catch Syntax

A screenshot of an asynchronous function that uses do-try-catch. Code available below.

Do-try catch syntax allows you to handle errors that can occur as an async-await request is carried out.

To make use of do-try-catch syntax, either wrap the code in a Task (as demonstrated in the basic use example), or make your function async as shown in the code below.

If you wish for the error to be passed on with the function, use throws. An example of throws is available in Apple's Meet StoreKit 2.

Any Questions ?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details