Async/await is a high level, structured model for concurrency in Swift that allows you to write expressive async code in shockingly little ceremony. A

Async/Await and the Future of Combine

submited by
Style Pass
2021-06-11 19:00:06

Async/await is a high level, structured model for concurrency in Swift that allows you to write expressive async code in shockingly little ceremony.

A code sample is probably best for this, so consider this example where we want to fetch an image showing the current weather conditions for the user’s location.

Nested completion callbacks each with their own failure path is a giant pile of yuck. If we fail to call back with a failure from a previous step we might end up with an application that seems to get stuck (maybe showing a spinner forever). There’s nothing the compiler does here to help us. And if we have to ensure to call back any completion handler on a specific queue, there’s quite a lot of boilerplate which makes the above code even worse.

Notice now the code reads from top to bottom. When we get to the await keyword, our execution is suspended without blocking the thread, so the system is free to do other work.

Leave a Comment