From e66b64e4e3e3409729befb31d894b89062e003dc Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Tue, 3 Aug 2021 09:31:08 +0100 Subject: [PATCH] _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. --- Sources/_NIOConcurrency/AsyncAwaitSupport.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/_NIOConcurrency/AsyncAwaitSupport.swift b/Sources/_NIOConcurrency/AsyncAwaitSupport.swift index 02dbb9c519..8d6d053163 100644 --- a/Sources/_NIOConcurrency/AsyncAwaitSupport.swift +++ b/Sources/_NIOConcurrency/AsyncAwaitSupport.swift @@ -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 { Task { do { let value = try await body()