Skip to content
/ Future Public

A futures and promises implementation for Swift.

License

Notifications You must be signed in to change notification settings

luoxiu/Future

Repository files navigation

Future.swift

travis release install platform license

Future.swift is a futures and promises implementation for Swift.

Highlights

  • Unlimited features
  • Unmatched performance
  • Friendly api
  • Type safe

Benchmark

The performance tests I'm using follows google/promises. The comparison libraries include promises, PromiseKit, BrightFutures and Hydra. In fact, promises is implemented in Objective-C, but whatever.

You can see benchmark/benchmark.xcodeporj for more information.

Average time in nanoseconds needed to create a resolved promise, chain 1/2/3 blocks and get into the last chained block on a serial queue (measured with 10,000 tries).

Average time in nanoseconds needed to resolve 10,000 pending promises with chained blocks and wait for control to get into each block on a concurrent queue.

Usage

Future.swift's api is very friendly, here is a real-wold demo:

func fetch(_ str: String) -> Future<HTTPResponse, HTTPError> {
    guard let url = URL(string: str) else {
        return .failure(.invalidURL(str))
    }

    let p = Promise<HTTPResponse, HTTPError>()
    URLSession.shared.dataTask(with: url) { (data, response, error) in
        if let e = error {
            p.fail(HTTPError.session(e))
            return
        }
        p.succeed(HTTPResponse(response, data)) 
    } 
    return p.future
}

let img = "https://cdn.io/me.png"
fetch(img)
    .validate {
        $0.status.isValid()
    }
    .userInitiated()
    .tryMap { 
        try ImageDecoder().decode($0.data)
    }
    .main { 
        self.imageView = $0
    }
    .background { 
        cache.add($0, for: img)
    }
    .catch { 
        Log.error($0)
    }

Future.swift's core interface is extremely simple, let's take a look:

Future

A future represents an eventual result of an asynchronous operation.

  • isPending: Return true if the future is pending.

  • isCompleted: Return true if the future is completed.

  • inspect(): Inspect the future atomically, return nil if the future is pending.

  • whenComplete(_ callback: @escaping (Result<Success, Failure>) -> Void): Add a callback to the future that will be called when the future is completed.

Promise

A promise is responsible for managing the state of a future.

let p = Promise<Result, Error>()

DispatchQueue.background {
    do {
        let r = try task()
        p.succeed(r)
    } catch let e {
        p.fail(e)
    }
}

p.future

Features

Future.swift provides 30+ methods to enhance future's capabilities:

  • always
  • and
  • any
  • asAny
  • asVoid
  • catch
  • delay
  • done
  • finally
  • flat
  • flatMap
  • hush
  • map
  • mute
  • pipe
  • race
  • recover
  • reduce
  • retry
  • return
  • some
  • tap
  • then
  • timeout
  • validate
  • wait
  • yield
  • whenAll
  • whenAny
  • ...

Detailed documentation is still being written, if you have good new ideas, welcome to contribute!

Install

dependencies: [
    .package(url: "https://github.com/luoxiu/Future.swift", from: "0.0.0")
]

License

MIT