Future

public typealias Future = EventLoopFuture
  • Adds a callback for handling this Future‘s result when it becomes available.

    futureString.do { string in
        print(string)
    }.catch { error in
        print("oops: \(error)")
    }
    

    Warning

    Don’t forget to use catch to handle the error case.

    Declaration

    Swift

    public func `do`(_ callback: @escaping (T) -> ()) -> Future<T>
  • Adds a callback for handling this Future‘s result if an error occurs.

    futureString.do { string in
        print(string)
    }.catch { error in
        print("oops: \(error)")
    }
    

    Note

    Will only be executed if an error occurs. Successful results will not call this handler.

    Declaration

    Swift

    public func `catch`(_ callback: @escaping (Error) -> ()) -> Future<T>
  • Adds a handler to be asynchronously executed on completion of this future.

    futureString.do { string in
        print(string)
    }.catch { error in
        print("oops: \(error)")
    }.always {
        print("done")
    }
    

    Note

    Will be executed on both success and failure, but will not receive any input.

    Declaration

    Swift

    public func always(_ callback: @escaping () -> ()) -> Future<T>
  • Statically available method for mimicking behavior of calling return future.map where no starting future is available.

    return Future.map(on: req) {
        return try someThrowingThing()
    }
    

    This allows you to convert any non-throwing, future-return method into a closure that accepts throwing and returns a future.

    Declaration

    Swift

    public static func map(on worker: Worker, _ callback: @escaping () throws -> Expectation) -> Future<Expectation>
  • Statically available method for mimicking behavior of calling return future.flatMap where no starting future is available.

    return Future.flatMap(on: req) {
        return try someAsyncThrowingThing()
    }
    

    This allows you to convert any non-throwing, future-return method into a closure that accepts throwing and returns a future.

    Declaration

    Swift

    public static func flatMap(on worker: Worker, _ callback: @escaping () throws -> Future<Expectation>) -> Future<Expectation>
  • Maps a Future to a Future of a different type.

    Note

    The result returned within should be non-Future type.

    print(futureString) // Future let futureInt = futureString.map(to: Int.self) { string in print(string) // The actual String return Int(string) ?? 0 } print(futureInt) // Future

    See flatMap(to:_:) for mapping Future results to other Future types.

    Declaration

    Swift

    public func map<T>(to type: T.Type, _ callback: @escaping (Expectation) throws -> T) -> Future<T>
  • Maps a Future to a Future of a different type.

    Note

    The result returned within the closure should be another Future.

    print(futureURL) // Future let futureRes = futureURL.flatMap(to: Response.self) { url in print(url) // The actual URL return client.get(url: url) // Returns Future } print(futureRes) // Future

    See map(to:_:) for mapping Future results to non-Future types.

    Declaration

    Swift

    public func flatMap<Wrapped>(to type: Wrapped.Type, _ callback: @escaping (Expectation) throws -> Future<Wrapped>) -> Future<Wrapped>
  • Calls the supplied closure if the chained Future resolves to an Error.

    The closure gives you a chance to rectify the error (returning the desired expectation) or to re-throw or throw a different error.

    The callback expects a non-Future return (if not throwing instead). See catchFlatMap for a Future return.

    Declaration

    Swift

    public func catchMap(_ callback: @escaping (Error) throws -> (Expectation)) -> Future<Expectation>
  • Calls the supplied closure if the chained Future resolves to an Error.

    The closure gives you a chance to rectify the error (returning the desired expectation) or to re-throw or throw a different error.

    The callback expects a Future return (if not throwing instead). See catchMap for a non-Future return.

     return conn.query("BEGIN TRANSACTION").flatMap {
         return transaction.run(on: connection).flatMap {
             return conn.query("END TRANSACTION")
         }.catchFlatMap { error in
             return conn.query("ROLLBACK").map {
                 throw error
             }
         }
     }
    

    Declaration

    Swift

    public func catchFlatMap(_ callback: @escaping (Error) throws -> (Future<Expectation>)) -> Future<Expectation>
  • Maps the current future to contain the new type. Errors are carried over, successful (expected) results are transformed into the given instance.

    user.save(on: req).transform(to: HTTPStatus.created)
    

    Declaration

    Swift

    public func transform<T>(to instance: T) -> Future<T>