{"id":22468268,"url":"https://github.com/alexhmelevski/ahfuture","last_synced_at":"2025-03-27T15:44:02.352Z","repository":{"id":56900487,"uuid":"90165439","full_name":"AlexHmelevski/AHFuture","owner":"AlexHmelevski","description":"Simple framework for asynchronous code in swift using Futures","archived":false,"fork":false,"pushed_at":"2020-02-15T19:00:58.000Z","size":101,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-08T13:47:27.217Z","etag":null,"topics":["functional-programming","futures","ios","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/AlexHmelevski.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":"2017-05-03T15:37:45.000Z","updated_at":"2020-02-15T19:01:01.000Z","dependencies_parsed_at":"2022-08-21T02:21:03.276Z","dependency_job_id":null,"html_url":"https://github.com/AlexHmelevski/AHFuture","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexHmelevski%2FAHFuture","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexHmelevski%2FAHFuture/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexHmelevski%2FAHFuture/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexHmelevski%2FAHFuture/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexHmelevski","download_url":"https://codeload.github.com/AlexHmelevski/AHFuture/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245874008,"owners_count":20686685,"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":["functional-programming","futures","ios","swift"],"created_at":"2024-12-06T11:16:03.623Z","updated_at":"2025-03-27T15:44:02.334Z","avatar_url":"https://github.com/AlexHmelevski.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AHFuture\n\n[![CI Status](http://img.shields.io/travis/AlexHmelevskiAG/AHFuture.svg?style=flat)](https://travis-ci.org/AlexHmelevskiAG/AHFuture)\n[![Version](https://img.shields.io/cocoapods/v/AHFuture.svg?style=flat)](http://cocoapods.org/pods/AHFuture)\n[![License](https://img.shields.io/cocoapods/l/AHFuture.svg?style=flat)](http://cocoapods.org/pods/AHFuture)\n[![Platform](https://img.shields.io/cocoapods/p/AHFuture.svg?style=flat)](http://cocoapods.org/pods/AHFuture)\n\nAHFuture is a concepts of asynchronous api using idea of futures.\n\nAHFuture  implements proven functional concepts in Swift to provide a powerful alternative to completion blocks and support typesafe error handling in asynchronous code.\n\n\n## Example\nWe write a lot of asynchronous code. Whether we're waiting for something to come in from the network or want to perform an expensive calculation off the main thread and then update the UI, we often do the 'fire and callback' dance. Here's a typical snippet of asynchronous code:\n\n```swift\nUser.logIn(username, password) { user, error in\n    if !error {\n        Posts.fetchPosts(user, success: { posts in\n            // do something with the user's posts\n        }, failure: handleError)\n    } else {\n        handleError(error) // handeError is a custom function to handle errors\n    }\n}\n```\n\nNow let's see what AHFuture can do for you:\n\n```swift\nUser.logIn(username, password).flatMap { user in\n    Posts.fetchPosts(user)\n}.onSuccess { posts in\n    // do something with the user's posts\n}.onFailure { error in\n    // either logging in or fetching posts failed\n}.execute()\n```\n\n## Supported operations: \n - [map](#map)\n - [flatMap](#`flatMap`)\n - [filter](#filter)\n - [retry](#retry)\n - [recover](#recover)\n - [run/observe](#run/observe)\n\n### `map`\nIf success will transfrom `User` response to `UserViewModel` that can be used in success block\n```swift\nUser.logIn(username, password).map { UserViewModel.init }\n.onSuccess { viewModel in\n    // do something with viewModel\n}.onFailure { error in\n    // either logging in or fetching posts failed\n}.execute()\n```\n\n### `flatMap`\n\nIf logIn succeed the response will be tranfromed to another AHFuture and the success block will contain result from the second AHFuture\n```swift\nUser.logIn(username, password).flatMap { Posts.fetchPosts }\n.onSuccess { posts in\n    // do something with the user's posts\n}.onFailure { error in\n    // either logging in or fetching posts failed\n}.execute()\n```\n\n### `filter`\nWill execute completion if predicate is true. \n```swift\nUser.logIn(username, password).flatMap { Posts.fetchPosts }\n\t\t\t      .filter { PostFilter.isTodaysPost }\n.onSuccess { posts in\n    // do something with the user's posts\n}.onFailure { error in\n    // either logging in or fetching posts failed\n}.execute()\n```\n### `retry`\nRetry operator will try to execute AHFuture if there is an error. The parametr shows the number of attempts\n```swift\nUser.logIn(username, password).flatMap { Posts.fetchPosts }\n\t\t\t      .retry(5)\n.onSuccess { posts in\n    // do something with the user's posts\n}.onFailure { error in\n    // either logging in or fetching posts failed\n}.execute()\n```\n\n### `recover`\nRecover operator helps transform error to a placeholder object.\n```swift\nUser.logIn(username, password).flatMap { Posts.fetchPosts }\n\t\t\t      .retry(5)\n                              .recover {ErrorHandler.transformToPlaceholderModel}\n.onSuccess { posts in\n    // do something with the user's posts\n}.onFailure { error in\n    // either logging in or fetching posts failed\n}.execute()\n```\n\n### `run/observe`\nSometimes we need to run our work on a particular thread and observe on another. For this purposes there is a `run` operator\n\n```swift\nCalculator.primeNumber(number)\n.run(on: myGlobalQueue)\n.observe(on: .main)\n.onSuccess { posts in\n    // do something with the user's posts\n}.onFailure { error in\n    // either logging in or fetching posts failed\n}.execute()\n```\n\n\n## Installation\n\nAHFuture is available through [CocoaPods](http://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod \"AHFuture\"\n```\n\n## TODO\n- add timeout operator\n- add synchronize/concat/zip operators\n\n\n## Author\n\nAlex Hmelevski, alexei.hmelevski@gmail.com\n\n## License\n\nAHFuture is available under the MIT license. See the LICENSE file for more info.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexhmelevski%2Fahfuture","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexhmelevski%2Fahfuture","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexhmelevski%2Fahfuture/lists"}