Asynchronous programming can be tricky, especially for less experienced developers. There are many things to consider like thread the code is executing on, priorities or deadlocks. There are also many tools and practices that make it easier to write asynchronous code.

In this post I would like to compare different styles of writing asynchronous code in Swift. Most of it also apply to Objective-C and other languages (like Java or C#) because concepts between platforms are shared very often. Main difference would be syntax and libraries used.

If you want to skip the basics jump to Promises section.

Let’s assume we are in common scenario where we have to download some data from a service and process it. Seems like an easy task, right? One could just write it like this:

let downloadedData = try! Data(contentsOf: url)
let image = UIImage(data: downloadedData)!
let processedImage = process(image)
// voilá!


Piotr Tobolski

iOS developer @ intive |