Promise of vapor

2019-08-03  本文已影响0人  hhcszgd

Promise

Most of the time, you will be transforming futures returned by calls to Vapor's APIs. However, at some point you may need to create a promise of your own.

To create a promise, you will need access to an EventLoop. All containers in Vapor have an eventLoop property that you can use. Most commonly, this will be the current Request.


/// Create a new promise for some string
let promiseString = req.eventLoop.newPromise(String.self)
print(promiseString) // Promise<String>
print(promiseString.futureResult) // Future<String>

/// Completes the associated future
promiseString.succeed(result: "Hello")

/// Fails the associated future
promiseString.fail(error: ...)

上一篇下一篇

猜你喜欢

热点阅读