_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.
This commit is contained in:
Si Beaumont authored and GitHub committed 2021-08-03 09:31:08 +01:00
1 parent 12ce699d88
commit e66b64e4e3
1 file changed
+3 -1
@@ -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()