EventLoopPromise

public struct EventLoopPromise<Value>

A promise to provide a result later.

This is the provider API for EventLoopFuture<Value>. If you want to return an unfulfilled EventLoopFuture<Value> – 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 = eventLoop.makePromise(of: ResultType.self)
    someAsyncOperationWithACallback(args) { result -> Void in
        // when finished...
        promise.succeed(result)
        // if error...
        promise.fail(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 .flatMap()
  • If you already have a value and need an EventLoopFuture<> object to plug into some other API, create an already-resolved object with eventLoop.makeSucceededFuture(result) or eventLoop.newFailedFuture(error:).

  • Note

    EventLoopPromise has reference semantics.

    • 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<Value>
    • Deliver a successful result to the associated EventLoopFuture<Value> object.

      Declaration

      Swift

      @inlinable
      public func succeed(_ value: Value)

      Parameters

      value

      The successful result of the operation.

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

      Declaration

      Swift

      @inlinable
      public func fail(_ error: Error)

      Parameters

      error

      The error from the operation.

    • Complete the promise with the passed in EventLoopFuture<Value>.

      This method is equivalent to invoking future.cascade(to: promise), but sometimes may read better than its cascade counterpart.

      Declaration

      Swift

      @inlinable
      public func completeWith(_ future: EventLoopFuture<Value>)

      Parameters

      future

      The future whose value will be used to succeed or fail this promise.

    • Complete the promise with the passed in Result<Value, Error>.

      This method is equivalent to invoking:

      switch result {
      case .success(let value):
          promise.succeed(value)
      case .failure(let error):
          promise.fail(error)
      }
      

      Declaration

      Swift

      @inlinable
      public func completeWith(_ result: Result<Value, Error>)

      Parameters

      result

      The result which will be used to succeed or fail this promise.