{"id":19687748,"url":"https://github.com/luoxiu/future","last_synced_at":"2025-07-07T05:02:38.265Z","repository":{"id":56911776,"uuid":"189441481","full_name":"luoxiu/Future","owner":"luoxiu","description":"A futures and promises implementation for Swift.","archived":false,"fork":false,"pushed_at":"2023-01-30T01:50:49.000Z","size":300,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-04T16:19:40.650Z","etag":null,"topics":["async","dispatch","fast","future","gcd","ios","macos","promise","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luoxiu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-30T15:49:50.000Z","updated_at":"2023-07-19T09:52:59.000Z","dependencies_parsed_at":"2023-02-16T02:35:19.784Z","dependency_job_id":null,"html_url":"https://github.com/luoxiu/Future","commit_stats":null,"previous_names":["luoxiu/future.swift"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/luoxiu/Future","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luoxiu%2FFuture","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luoxiu%2FFuture/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luoxiu%2FFuture/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luoxiu%2FFuture/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luoxiu","download_url":"https://codeload.github.com/luoxiu/Future/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luoxiu%2FFuture/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264016713,"owners_count":23544623,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","dispatch","fast","future","gcd","ios","macos","promise","swift"],"created_at":"2024-11-11T18:36:05.524Z","updated_at":"2025-07-07T05:02:38.248Z","avatar_url":"https://github.com/luoxiu.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Future.swift\n\n![travis](https://img.shields.io/travis/luoxiu/Future.svg)\n![release](https://img.shields.io/github/v/release/luoxiu/Future?include_prereleases)\n![install](https://img.shields.io/badge/install-spm%20%7C%20cocoapods%20%7C%20carthage-ff69b4)\n![platform](https://img.shields.io/badge/platform-ios%20%7C%20macos%20%7C%20watchos%20%7C%20tvos%20%7C%20linux-lightgrey)\n![license](https://img.shields.io/github/license/luoxiu/Future.swift?color=black)\n\n\n`Future.swift` is a [futures and promises](https://en.wikipedia.org/wiki/Futures_and_promises) implementation for Swift.\n\n\u003cimg src=\"logo.png\" width=\"300px\"\u003e\n\n## Highlights\n\n- **Unlimited features**\n- **Unmatched performance**\n- **Friendly api**\n- **Type safe**\n\n## Benchmark\n\nThe performance tests I'm using follows [google/promises](https://github.com/google/promises/blob/master/g3doc/index.md#benchmark). The comparison libraries include [promises](https://github.com/google/promises), [PromiseKit](https://github.com/mxcl/PromiseKit), [BrightFutures](https://github.com/Thomvis/BrightFutures) and [Hydra](https://github.com/malcommac/Hydra). In fact, [promises](https://github.com/google/promises) is implemented in Objective-C, but whatever.\n\nYou can see [benchmark/benchmark.xcodeporj](https://github.com/luoxiu/Future.swift/tree/master/benchmark) for more information. \n\n\u003e 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).\n\n\u003cimg src=\"resources/benchmark-serial.png\" width=\"800px\"\u003e\n\n\u003e 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.\n\n\u003cimg src=\"resources/benchmark-concurrent.png\" width=\"600px\"\u003e\n\n## Usage\n\n`Future.swift`'s api is very friendly, here is a real-wold demo:\n\n```swift\nfunc fetch(_ str: String) -\u003e Future\u003cHTTPResponse, HTTPError\u003e {\n    guard let url = URL(string: str) else {\n        return .failure(.invalidURL(str))\n    }\n\n    let p = Promise\u003cHTTPResponse, HTTPError\u003e()\n    URLSession.shared.dataTask(with: url) { (data, response, error) in\n        if let e = error {\n            p.fail(HTTPError.session(e))\n            return\n        }\n        p.succeed(HTTPResponse(response, data)) \n    } \n    return p.future\n}\n\nlet img = \"https://cdn.io/me.png\"\nfetch(img)\n    .validate {\n        $0.status.isValid()\n    }\n    .userInitiated()\n    .tryMap { \n        try ImageDecoder().decode($0.data)\n    }\n    .main { \n        self.imageView = $0\n    }\n    .background { \n        cache.add($0, for: img)\n    }\n    .catch { \n        Log.error($0)\n    }\n```\n\n`Future.swift`'s core interface is extremely simple, let's take a look:\n\n\n### Future\n\nA future represents an eventual result of an asynchronous operation.\n\n- `isPending`: Return true if the future is pending.\n\n- `isCompleted`: Return true if the future is completed.\n\n- `inspect()`: Inspect the future atomically, return nil if the future is pending.\n\n- `whenComplete(_ callback: @escaping (Result\u003cSuccess, Failure\u003e) -\u003e Void)`: Add a callback to the future that will be called when the future is completed.\n\n### Promise\n\nA promise is responsible for managing the state of a future.\n\n```swift\nlet p = Promise\u003cResult, Error\u003e()\n\nDispatchQueue.background {\n    do {\n        let r = try task()\n        p.succeed(r)\n    } catch let e {\n        p.fail(e)\n    }\n}\n\np.future\n```\n\n### Features\n\n`Future.swift` provides 30+ methods to enhance future's capabilities: \n\n- `always`\n- `and`\n- `any`\n- `asAny`\n- `asVoid`\n- `catch`\n- `delay`\n- `done`\n- `finally`\n- `flat`\n- `flatMap`\n- `hush`\n- `map`\n- `mute`\n- `pipe`\n- `race`\n- `recover`\n- `reduce`\n- `retry`\n- `return`\n- `some`\n- `tap`\n- `then`\n- `timeout`\n- `validate`\n- `wait`\n- `yield`\n- `whenAll`\n- `whenAny`\n- ... \n\nDetailed documentation is still being written, if you have good new ideas, welcome to contribute!\n\n## Install\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/luoxiu/Future.swift\", from: \"0.0.0\")\n]\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluoxiu%2Ffuture","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluoxiu%2Ffuture","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluoxiu%2Ffuture/lists"}