Async await in Swift explained

Reading Time: 5 minutes

Async await?

Async await is part of the new structured concurrency changes which were launched in Swift 5.5 during WWDC 2021. As we already know Concurrency in Swift means allowing multiple pieces of code to run at the same time, and is very important for the performance of our apps. With the new Async await, we can define methods performing work asynchronously.

Now that “Async await” is finally here, we can simplify our code with async methods and await statements and make our asynchronous code easier to read.

Async is?

Async is asynchronous and can be seen as a method attribute, which method performs asynchronous work. As an example we can use the method below:

func getProducts() async throws -> [Products] {
    // ... perform here data request
}

The getProducts method is defined as async throwing, which means that it’s performing a fail-able asynchronous job. The method would return an array of custom objects Product if everything went well or would throw an error if something went wrong.

How async can replace completion callbacks

Async methods replace the often seen completion callbacks. Completion callbacks (closure) were common in Swift to return from an asynchronous task, often combined with a Result type parameter. The above method would have been written as followed:

func getProducts(completion: (Result<[Product], Error>) -> Void) {
    // ... perform here data request
}

Creating a method with a completion closure is still possible in Swift, but it has a few disadvantages which are solved by using async instead:

  • Developer has to make sure to call the completion closure in each possible method exit, if not doing so will possibly result in an app waiting for a result infinitely.
  • Callbacks (closures) are harder to read. It’s not as easy to read about the order of execution as compared to how easy it is with structured concurrency.
  • Retain cycles has to be avoided using weak references.
  • Implementers has to switch over the result to get the outcome, also it’s not possible to use try catch statements.

These disadvantages are based on the closure version using the relatively new Result enum. It’s likely that a lot of projects still make use of completion callbacks without this enumeration:

func getProducts(completion: ([Product]?, Error?) -> Void) {
    // .. perform here data request
}

Defining a method like this makes it even harder to reason about the outcome on the caller’s side. Here value and error are optional, which requires us to perform an unwrap in any case. Unwrapping these optional results(value and error) in more code clutter does not help to improve readability.

How await works?

Await as the keyword stays to be used for calling async methods. Usually, we see them as best friends in Swift as one will never go without the other. You can basically say:

Await is awaiting a callback from its buddy async

Even though this sounds childish, it’s not a lie! If we take a look at an example by calling our earlier defined async throwing fetch products method:

do {
    let products = try await getProducts()
    print("Got \(products.count) products.")
} catch {
    print("Getting products failed with error \(error)")
}

We can note that the above code example is performing an asynchronous task. Using the await keyword, we tell our program to await a result from the getProducts method and only continue after a result arrives. This could either be an array of products or an error if anything went wrong while fetching the products.

What is structured concurrency?

Structured concurrency with async-await method calls makes it easier to understand the order of execution. Methods are linearly executed, one by one, without going back and forth like you would with closures.

To understand this better, we will take a look at how we would call the above code example before structured concurrency arrived:

// 1. Call the method

getProducts { result in

    // 3. The asynchronous method return

    switch result {

    case .success(let products):

        print(“Got \(products.count) products.”)

    case .failure(let error):

        print(“Getting products failed with error \(error)”)

    }

}

// 2. The calling method exits

As you can see, the calling method returns before the products are fetched. In case a result is received, we go back into our flow within the completion callback. This is an unstructured order of execution and could be hard to understand. This is especially true if we would perform another asynchronous method within our completion callback which would add another closure callback:

// 1. Call the method
getProducts { result in
    // 3. The asynchronous method return
    switch result {
    case .success(let products):
        print("Got \(products.count) products.")
        
        // 4. Call the placed method
        placedProducts(products) { result in
            // 6. Placed method returns
            switch result {
            case .success(let products):
                print("Decoded \(products) products.")
            case .failure(let error):
                print("Decoding products failed with error \(error)")
            }
        }
        // 5. Fetch products method returns
    case .failure(let error):
        print("Getting products failed with error \(error)")
    }
}
// 2. The calling method exits

Each completion callback (closure) adds another level of indentation, which makes it harder to follow the order of execution.

If we rewrite the above code using async-await syntax, we have a more readable piece of code, and also explains best what structured concurrency does:

do {
    // 1. Call the method
    let products = try await getProducts()
    // 2. Fetch products method returns
    
    // 3. Call the placed method
    let placedProducts = try await placedProducts(products)
    // 4. Placed method returns
    
    print("Got \(products.count) products.")
} catch {
    print("Getting products failed with error \(error)")
}
// 5. The calling method exits

The order of execution is linear, easy to follow and easy to reason about. Asynchronous calls will be easier to understand while we’re still performing sometimes complex asynchronous tasks.