Skip to content

Commit e66b64e

Browse files
_NIOConcurrency: Rename completeWithAsync(_:) to completeWithTask(_:) 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.
1 parent 12ce699 commit e66b64e

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Sources/_NIOConcurrency/AsyncAwaitSupport.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ extension EventLoopPromise {
6262
///
6363
/// - parameters:
6464
/// - body: The `async` function to run.
65+
/// - returns: A `Task` which was created to `await` the `body`.
6566
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
67+
@discardableResult
6668
@inlinable
67-
public func completeWithAsync(_ body: @escaping () async throws -> Value) {
69+
public func completeWithTask(_ body: @escaping () async throws -> Value) -> Task<Void, Never> {
6870
Task {
6971
do {
7072
let value = try await body()

0 commit comments

Comments
 (0)