EventLoopPromise

public struct EventLoopPromise<T>

A promise to provide a result later.

This is the provider API for EventLoopFuture<T>. If you want to return an unfulfilled EventLoopFuture<T> – presumably because you are interfacing to some asynchronous service that will return a real result later, follow this pattern:

func someAsyncOperation(args) -> EventLoopFuture<ResultType> {
    let promise: EventLoopPromise<ResultType> = eventLoop.newPromise()
    someAsyncOperationWithACallback(args) { result -> Void in
        // when finished...
        promise.succeed(result: result)
        // if error...
        promise.fail(error: error)
    }
    return promise.futureResult
}

Note that the future result is returned before the async process has provided a value.

It’s actually not very common to use this directly. Usually, you really want one of the following:

  • If you have an EventLoopFuture and want to do something else after it completes, use .then()
  • If you just want to get a value back after running something on another thread, use EventLoopFuture<ResultType>.async()
  • If you already have a value and need an EventLoopFuture<> object to plug into some other API, create an already-resolved object with eventLoop.newSucceededFuture(result) or eventLoop.newFailedFuture(error:).
  • The EventLoopFuture which is used by the EventLoopPromise. You can use it to add callbacks which are notified once the EventLoopPromise is completed.

    Declaration

    Swift

    public let futureResult: EventLoopFuture<T>
  • Deliver a successful result to the associated EventLoopFuture<T> object.

    Declaration

    Swift

    public func succeed(result: T)

    Parameters

    result

    The successful result of the operation.

  • Deliver an error to the associated EventLoopFuture<T> object.

    Declaration

    Swift

    public func fail(error: Error)

    Parameters

    error

    The error from the operation.