Then: Elegant Async Swift

Then

Then is a lightweight, pure Swift library that provides an elegant way to manage asynchronous code, liberating you from the complexities of nested callbacks, often referred to as "Callback Hell". It is built on the popular Promise / Future concept and includes modern Async/Await style syntax.

Async code is often hard to write, hard to read, and difficult to reason about. Then simplifies this by enabling you to write asynchronous operations that read like a clear, concise English sentence.

The Problem: Callback Hell

Traditional asynchronous programming with nested completion handlers can quickly become unmanageable.

Before Then:

fetchUserId({ id in
    fetchUserNameFromId(id, success: { name in
        fetchUserFollowStatusFromName(name, success: { isFollowed in
            // The three calls in a row succeeded YAY!
            reloadList()
        }, failure: { error in
            // Fetching user ID failed
            reloadList()
        })
    }, failure: { error in
        // Fetching user name failed
        reloadList()
    })
}) {  error in
    // Fetching user follow status failed
    reloadList()
}

This code is hard to follow, error-prone, and difficult to maintain.

The Solution: Elegant Chaining

After using Then:

fetchUserId()
    .then(fetchUserNameFromId)
    .then(fetchUserFollowStatusFromName)
    .then(updateFollowStatus)
    .onError(showErrorPopup)
    .finally(reloadList)

This version is concise, flexible, maintainable, and reads like a natural sentence. It cleanly separates the success path, error handling, and final cleanup steps.

Key Features

  • Promise-Based: Built on the robust Promise/Future pattern.
  • Async/Await Syntax: Write asynchronous code in a synchronous, linear style.
  • Rich Feature Set: Includes powerful helpers like progress, race, whenAll, recover, validate, retry, and more.
  • Strongly Typed: Leverages Swift's type system to catch errors at compile time.
  • Pure Swift & Lightweight: No external dependencies, keeping your project lean.

Ready to get started? Head over to the Installation guide and then the Quick Start tutorial.