Skip to content

Commit

Permalink
_NIOConcurrency: Rename completeWithAsync(_:) to completeWithTask(_:)…
Browse files Browse the repository at this point in the history
… and return task (#1911)

Return `Task` from `EventLoopPromise.completeWithAsync(_:)` wrapper.

Motivation:

Bridging an `async` function into NIO is done by calling `completeWithAsync(_ body:)` on an `EventLoopPromise`. This spins up a `Task` and `await`s the result of the `async` closure that was passed in and uses the result to fulfil the promise with the value of the function, if it was successful, or with the error, if the function threw.

In some cases we may want to cancel this operation from the outside.

Modifications:

This patch adds a discardable return value to `EventLoopPromise.completeWithAsync(_:)` which is the `Task` it creates.

Result:

Users of `EventLoopPromise.completeWithAsync(_:)` are now able to explicitly cancel the `Task` that is being used to fulfil the promise.
  • Loading branch information
simonjbeaumont authored Aug 3, 2021
1 parent 12ce699 commit e66b64e
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Sources/_NIOConcurrency/AsyncAwaitSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ extension EventLoopPromise {
///
/// - parameters:
/// - body: The `async` function to run.
/// - returns: A `Task` which was created to `await` the `body`.
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@discardableResult
@inlinable
public func completeWithAsync(_ body: @escaping () async throws -> Value) {
public func completeWithTask(_ body: @escaping () async throws -> Value) -> Task<Void, Never> {
Task {
do {
let value = try await body()
Expand Down

0 comments on commit e66b64e

Please sign in to comment.