PromiseKit, a very easy introduction

Photo by Levi Alvarez on Unsplash

PromiseKit is a library that helps with asynchronous programming in swift and Objective-C. It is a wrapper around async task. Instead of using completion and error handler, a method can return a Promise. It is explained as “PromiseKit is a thoughtful and complete implementation of promises” in the official doc.

Advantages of using PromiseKit over traditional iOS async programming:

  • Quick learning curve
  • Very easy to call multiple async functions and managing it
  • Save us from Callbacks Pyramid of Doom. Nested callbacks are now in the chain.
  • Error Handling is separated. Duplicate multiple error handlings are in one place.

Example:

Common Syntax:

☐ then and done:

Example:

catch:

Example:

ensure:

Example:

☐ when:

Example:

☐ map, compactMap etc:

Example:

☐ firstly:

Example:

☐ Creating a promise:

☐ Threading:

Example:

Do I need to use [weak self] in PromiseKit block?

Thanks and additional resources: