Giter Club home page Giter Club logo

Comments (9)

Thomvis avatar Thomvis commented on June 11, 2024

Hi @RahavLussato. BrightFutures uses typed errors and therefore doesn't directly work with Swift's untyped throws. The alternative is to use the flatMap variant that allows you to return a Result: https://github.com/Thomvis/BrightFutures/blob/master/Sources/BrightFutures/AsyncType+ResultType.swift#L72.

from brightfutures.

RahavLussato avatar RahavLussato commented on June 11, 2024

Thanks @Thomvis , can you help me how should i write this ?
i tried like this but its not compiling, "Cannot invoke 'flatMap' with an argument list of type '(@escaping (JSON) -> Result)'"

return client.call(method: method, params: json).flatMap { (json:JSON) -> Result<JSON> in
                var result: Result<JSON, Error>
                
                do {
                    let response = try self.decoder.decode(responseType, from: json.rawData())
                    DDLogDebug("call process \(json)")
                    result = response
                }
                catch let error {
                    DDLogError("Parse \(method) response failed: \(json) with error \(error.localizedDescription)")
                    result = error
                }
                
                return result
            }

from brightfutures.

Thomvis avatar Thomvis commented on June 11, 2024

A couple of things:

  • the result type of your closure is defined as Result<JSON> but should be Result<JSON, Error>
  • when you assign response to result, you need to use the correct result enum case, so result = .success(response)
  • same goes for the error case (wrap it in .failure)

from brightfutures.

RahavLussato avatar RahavLussato commented on June 11, 2024

Thanks @Thomvis but still getting errors :(

  1. Cannot invoke 'flatMap' with an argument list of type '(@escaping (JSON) -> <>)'
  2. Generic type 'Result' specialized with too many type parameters (got 2, but expected 1)
return client.call(method: method, params: json).flatMap { (json:JSON) -> Result<JSON, Error> in
                var result: Result<JSON, Error>
                
                do {
                    let response = try self.decoder.decode(responseType, from: json.rawData())
                    DDLogDebug("call process \(json)")
                    result = .success(response)
                }
                catch let error {
                    DDLogError("Parse \(method) response failed: \(json) with error \(error.localizedDescription)")
                    result = .failure(response)
                }
                
                return result
            }

from brightfutures.

RahavLussato avatar RahavLussato commented on June 11, 2024

Screen Shot 2020-03-12 at 11 00 53

from brightfutures.

Thomvis avatar Thomvis commented on June 11, 2024

It seems like the compiler is confused about which Result type to use. Do you or any of your dependencies declare one with a single type parameter?

from brightfutures.

RahavLussato avatar RahavLussato commented on June 11, 2024

nothing that I can find, can I specify it?

from brightfutures.

RahavLussato avatar RahavLussato commented on June 11, 2024

now it compiled :)

return client.call(method: method, params: json).flatMap { (json:JSON) -> Swift.Result<T, Error> in
                var result: Swift.Result<T, Error>
                
                do {
                    let response = try self.decoder.decode(responseType, from: json.rawData())
                    DDLogDebug("call process \(json)")
                    result = .success(response)
                }
                catch let error {
                    DDLogError("Parse \(method) response failed: \(json) with error \(error.localizedDescription)")
                    result = .failure(error)
                }
                
                return result
            }

from brightfutures.

RahavLussato avatar RahavLussato commented on June 11, 2024

@Thomvis so does it going to return to the right callback (onSuccess / onFailure) by the type of the result ?

from brightfutures.

Related Issues (20)

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.