PromiseKit

Make cleaner and clearer asynchronous code in Swift

Sergio Ordine
Academy@EldoradoCPS
2 min readJan 27, 2019

--

Promises are data structures that promise you a value of a given type when it is ready and let you handle this result. It makes easy to handle success and error as well as chaining and synchronizing asynchronous processes.

This is not a recent concept, as promises were proposed in 1976 by Daniel P. Friedman and David Wise. Similar concepts as eventual and future were proposed by Peter Hibbard also in 1976 and Henry Baker and Carl Hewitt in 1977. This concept is used in several concurrent programming languages.

There is a framework for promises in iOS called PromiseKit that is very active and being developed since 2014.

Why?

Let's make a function that gets a single fact about cats using the Cat Facts API:

Asynchronous function to get a cat fact

And the call to this function:

We can see that there is a lot of error handling code mixed with our application logic (in this case, just parse the JSON) in the catFact function. The completion block also makes us deal with optional values for both the result and error and handle them with additional code in our control flow (the if-let statements). It generates many nested levels of code that makes hard to maintain and understand, what makes it sometimes be called "pyramids of doom".

Let's check how the same code would be using PromiseKit (and a promise extension, that is part of PromiseKit, for URLSession):

And its is called as:

Comparing both codes we can see that the promise based code is shorter and has less error handling statements. It happens because errors are passed down the promise chain without the need of explicit code (as we did in the first example). Check also that we handle just data and response coming from the dataTask, not error.

Comparing also the call to catFact, we can see that the completion for success (under the done function) is logically separated from the error handling (under the catch function). It is said that when a promise finish executing (that is called as resolved), it can either success (when it is said that the promise is fulfilled) or being erred (when it is said that the promise was rejected).

A list of advantages of using promises is:

  • It is easier and cleaner to chain asynchronous calls and its results;
  • Error handling is logically separated from the success data flow;
  • There is not need for Optionals to handle errors and values just because they could not be defined (due to the same flow handling error and success);
  • The characteristics above make us write less boilerplate code;
  • It usually generates a more concise, readable and clean code.

In the next part of this article, I will show how to create promises using PromiseKit.

--

--

Sergio Ordine
Academy@EldoradoCPS

Software developer and educator. If you want to support me and my content production, please buy me a beer at https://www.buymeacoffee.com/sergioordine