mirror of https://github.com/vapor/docs.git
responder docs
This commit is contained in:
parent
b639067bc1
commit
a8a628bc14
|
|
@ -121,6 +121,21 @@ futureUsername.then { username in
|
|||
}
|
||||
```
|
||||
|
||||
## Futures without promise
|
||||
|
||||
In some scenarios you're required to return a `Future` where a `Promise` isn't necessary as you already have the result.
|
||||
|
||||
In these scenarios you can initialize a future with the already completed result.
|
||||
|
||||
```swift
|
||||
// Already completed on initialization
|
||||
let future = Future("Hello world!")
|
||||
|
||||
future.then { string in
|
||||
print(string)
|
||||
}
|
||||
```
|
||||
|
||||
## For synchronous APIs
|
||||
|
||||
Sometimes, an API needs to be used synchronously in a synchronous envinronment.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
# Responder
|
||||
|
||||
Responders are a type capable of [responding](response.md) to a [Request](request.md).
|
||||
|
||||
Responders are always [async](../async/promise-future.md) by returning a `Future<Response>` by either transforming/mapping an existing future or creating it's own promise.
|
||||
|
||||
## Implementing a static Responder
|
||||
|
||||
```swift
|
||||
struct StaticResponder: Responder {
|
||||
let response: Response
|
||||
|
||||
init(response: Response) {
|
||||
self.response = response
|
||||
}
|
||||
|
||||
func respond(to req: Request) throws -> Future<Response> {
|
||||
return Future(response)
|
||||
}
|
||||
}
|
||||
```
|
||||
Loading…
Reference in New Issue