Swift — Write beautiful Async code, with Promises

Async code sucks…

Sacha Durand Saint Omer
Yummypets Developers
2 min readMar 5, 2016

--

It is hard to write, hard to read, hard to reason about.
A pain to maintain

Enters then 🎬

By using a then keyword that enables you to write aSync code that reads like an English sentence.

Async code becomes concise, flexible, maintainable. And dare I say beautiful ❤️

Writing your own Promise 💪

Wondering what fetchUserId() is?
It is a simple function that returns a strongly typed promise :

Here you would typically replace the dummy doSomethingAsync function by your network request ❤

Now you can use it like so :

Bonus 🤓

If we want this to be beautiful, it should read like an english sentence
We can do this by extracting our blocks into separate functions :

#goodbyeCallbackHell

For more info checkout the github repo here :

then 🎬, an Elegant Async code for Swift library

More goodness ❤️

then is part of a series of lightweight libraries aiming to make developing iOS Apps a breeze :

  • View Layout : Stevia 🍃
  • Json Parsing : Arrow 🏹
  • JSON WebServices : ws ☁️

--

--

Sacha Durand Saint Omer
Yummypets Developers

Avid learner, iOS Engineer @Yummypets, ❤ Swift, Health, Languages & Music