responder docs

This commit is contained in:
Joannis Orlandos 2017-09-11 13:03:31 +02:00
parent b639067bc1
commit a8a628bc14
2 changed files with 36 additions and 0 deletions

View File

@ -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.

View File

@ -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)
}
}
```