Skip to content

Instantly share code, notes, and snippets.

@betamatt
Created June 27, 2015 19:16
Show Gist options
  • Save betamatt/e9f0910d3023522fc43d to your computer and use it in GitHub Desktop.
Save betamatt/e9f0910d3023522fc43d to your computer and use it in GitHub Desktop.
Fully async snell controller action
def update
user = User.find(1)
if user.update_attributes(params[:user])
respond_with(@user)
else
respond_with(@user.errors, :status => :unprocessable_entity)
end
end
func update() -> Promise<Response> {
// Swift's current level of suck at type inference and closures makes this more verbose than
// would otherwise be expected. It should be solvable in llvm and hopefully the situation improves.
return User.find(1)
.then({ (user:Repository) -> Promise<Response> in
let user = user as! User
return user.update_attributes(["name": "Test"])
.then({ (valid:Bool) -> Response in
if valid {
return Response(status: 200, body: "User updated")
} else {
return Response(status: 422, body: "Validation errors")
}
})
})
}
@jonklein
Copy link

Looks sensible, but yes, verbose.

And I'm wondering how to expose hooks to reduce the boilerplate and handle common failures. For example, implicit in the Rails version is rescuing ActiveRecord::RecordNotFound with a sensible 404 -- we're not even capturing that here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment