Skip to content

Instantly share code, notes, and snippets.

@cweinberger
Last active February 21, 2021 21:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cweinberger/93d4b214d4eebf6ec01f173d242f8075 to your computer and use it in GitHub Desktop.
Save cweinberger/93d4b214d4eebf6ec01f173d242f8075 to your computer and use it in GitHub Desktop.
Vapor 4 Utilities
import NIO
/// Author: vzsg (Discord) - https://twitter.com/vzsg_dev
/// Source: https://discordapp.com/channels/431917998102675485/684159753189982218/684537099378098272
extension EventLoopFuture {
func tryFlatMap<NewValue>(
file: StaticString = #file,
line: UInt = #line,
_ callback: @escaping (Value) throws -> EventLoopFuture<NewValue>
) -> EventLoopFuture<NewValue> {
return flatMap(file: file, line: line) { result in
do {
return try callback(result)
} catch {
return self.eventLoop.makeFailedFuture(error, file: file, line: line)
}
}
}
}
import Vapor
extension ViewRenderer {
public func render<E>(_ name: String, _ context: EventLoopFuture<E>) -> EventLoopFuture<View>
where E: Encodable
{
context.flatMap { context in
self.render(name, context)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment