lukevanin / SwiftPromise

Future / Promise pattern in Swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SwiftPromise

Micro framework for using Futures and Promises in Swift.

More examples and documentation coming soon. Check out Promise.swift and the playground example to get started.

Pull requests welcome.

Example:

func zen() -> Promise<String> {

    // Create a future.
    let f = Future<String>()

    // Do some asynchronous work.
    let url = URL(string: "https://api.github.com/zen")!

    let task = URLSession.shared().dataTask(with: url) { data, response, error in

        // Resolve the future with a value or error.
        if let data = data {
            let value = String(data: data, encoding: String.Encoding.utf8)
            f.resolve(value: value!)
        }
        else {
            f.resolve(error: error!)
        }
    }

    task.resume()

    // Create the promise.
    return f.promise()
}

// Handle the result.
zen().map() {
    print("Done: \($0)")
}

About

Future / Promise pattern in Swift.

License:MIT License


Languages

Language:Swift 97.9%Language:Objective-C 2.1%