GithubHelp home page GithubHelp logo

fpg1503 / asynchronous Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 1.0 856 KB

Implementation-agnostic asynchronous code

License: MIT License

Swift 100.00%
swift cocoapods promises promise-library futures asynchronous async-programming swift-4 agnostic-implementation tested

asynchronous's Introduction

Asynchronous

CI Status Version License Platform Documentation codecov

Asynchronous is a one-stop shop for your async needs, the user can use the subspecs to automatically run the Async code using completion handlers, BrightFutures, HydraAsync, PromiseKit, Promises, then and much more!

The ultimate answer to which asynchronous abstraction should I use in my API?, just use Asynchronous and give your users freedom without the headache of adding several dependencies and different abstractions to your code!

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Sample use case

Let's say you're writing a mobile SDK that does asynchronous tasks (such as HTTP requests), just like the following:

func getUser(by id: String) -> Async<User> {
    return Async { resolve, reject in
        let url = APIRouter.route(for: .users, id: id)
        let request = URLRequest(url: url)

        let session = URLSession.shared
        let task = session.dataTask(with: request) { data, response, error in
            if let error = error {
                reject(error) // You can also `throw error`!
            } else if let data = data {
                do {
                    let user = try JSONDecoder().decode(User.self, from: data)
                    resolve(user)
                } catch (let error) {
                    reject(error)
                }
            } else {
                reject(NSError(domain: "my.domain", code: 123))
            }
        }
        task.resume()
    }
}

That's it! You simply create an Async and call resolve or reject once your task is finished.

The user can either add a completion block or use their favorite asynchronous abstraction:

Completion

apiClient.getUser(by: id).async { user, error in
    if let user = value {
        userName.text = user.name
    } else {
        display(error: error)
    }
}

Promise

apiClient.getUser(by: id).promise()
.then { user in
    userName.text = user.name
}.catch { error in
    display(error: error)
}

Installation

Async is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Asynchronous'

Available Subspecs

Name Description
Asynchronous/Alamofire Alamofire support
Asynchronous/BrightFutures BrightFutures support
Asynchronous/HydraAsync HydraAsync support
Asynchronous/PromiseKit PromiseKit support
Asynchronous/Promises Promises support
Asynchronous/Then then support

Contributing

Contributions are encourajed and appreciated! For more information take a look at CONTRIBUTING.md.

Roadmap

  • ReactiveSwift support
  • RxSwift support
  • Tests
  • Better documentation
  • Improved README
  • Remove BrightFutures dependency
  • Carthage support
  • SwiftPM support
  • Fix unnecessary error type erasures
  • Fix tuple gate in Asyncify
  • Improve Example project

Author

Francesco Perrotti-Garcia, [email protected]

License

Asynchronous is available under the MIT license. See the LICENSE file for more info.

asynchronous's People

Contributors

fpg1503 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

spartakb

asynchronous's Issues

Alamofire tests failing

Hey!

I was showing your project to my team and I found some failing tests.

So, I cloned the project and ran the tests and some tests on AlamofireAsyncTests failed.
I notice that the failing tests are those that check for failing responses, with the message saying that expect TestError but got nil instead.

I tried to fix it with the code below

let testError = NSError(domain: "AsynchronousTests", code: 0, userInfo: nil)

func testDataRequestResponseFailure() {
    let expected = testError

    stub(everything, failure(expected as NSError))

    wait { expectation in
        Alamofire.request(fakeURL).response().async { result in
            switch result {
            case .success(let (_, _, value)):
                XCTFail("Unexpected value \(value)")
            case .failure(let actual):
                XCTAssertEqual(expected, actual.error as NSError)
            }
            expectation.fulfill()
        }
    }
}

but got this error

XCTAssertEqual failed: ("Error Domain=AsynchronousTests Code=0 "(null)"") is not equal to ("Error Domain=AsynchronousTests Code=0 "(null)" UserInfo={_NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <0063E615-BE9B-4022-AC07-1F06C14BDF97>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <0063E615-BE9B-4022-AC07-1F06C14BDF97>.<1>}")

So I think Mockingjay is returning another error or something like this. I looked at Mockingjay tests and here they are just verifying the error's domain.
Should we do the same maybe?

Also, I tried calling super.setUp here but the results were the same.

I running with this environment

Xcode 11.2.1
Build version 11B500

Could you please take a look at this?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.