Giter Site home page Giter Site logo

Asynchronous about vapor HOT 8 CLOSED

elliottminns avatar elliottminns commented on April 27, 2024
Asynchronous

from vapor.

Comments (8)

tanner0101 avatar tanner0101 commented on April 27, 2024

I like this idea. I'd like to continue to support the current interface, though. I think the async response is less common.

Possibly a Route.getAsync() or Route.get().async() API would work.

from vapor.

elliottminns avatar elliottminns commented on April 27, 2024

It should be possible to support both, without having different interfaces defined, using function overloading.

// Sync
Router.post("") { request in
    return Response()
}

// Async
Router.post("") { request, response in 
    response.send()
}

from vapor.

tanner0101 avatar tanner0101 commented on April 27, 2024

I'm not at a computer right now to see if Swift would complain about the params to the closure not matching. But I know it will complain if the closure doesn't return a Response..

Possibly an AsyncResponse object could be returned with a closure inside.

from vapor.

tanner0101 avatar tanner0101 commented on April 27, 2024

@elliottminns Just saw the note about function overloading. You're right. I think that would work.

from vapor.

elliottminns avatar elliottminns commented on April 27, 2024

Awesome, I'll get started on this on my fork.

from vapor.

elliottminns avatar elliottminns commented on April 27, 2024

I've managed to implement this however with one caveat. I get this build error for using my old sync methods now, such as

Route.get("/") { request in
    let view = try View(path: "Views/index.html")
    return view
}
/Project/Sources/main.swift:9:1: error: ambiguous use of 'get(_:closure:)'
Route.get("/") { request in
^
Vapor.Route:4:23: note: found this candidate
    public class func get(path: String, closure: Vapor.Route.Type.Closure)
                      ^
Vapor.Route:5:23: note: found this candidate
    public class func get(path: String, closure: Vapor.Route.Type.ResponseClosure)
                      ^
<unknown>:0: error: build had 1 command failures

It seems that while the async method works fine, the interface for the sync method is now too ambiguous for the compiler. This can be solved by explicitly defined which closure type it is, however this does take away from the cleanliness of the original interface and will be breaking for any current code:

Route.get("/") { (request) -> ResponseConvertible in
    let view = try View(path: "Views/index.html")
    return view
}   

It may be cleaner therefore to instead add a Route.async.get() or Route.getAsync() method instead to preserve the original interface.

Let me know your thoughts

from vapor.

tanner0101 avatar tanner0101 commented on April 27, 2024

Can you create a pull request with the changes?

from vapor.

tanner0101 avatar tanner0101 commented on April 27, 2024

6b033df fixes this.

from vapor.

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.