Skip to content

Commit 44000fd

Browse files
committed
[Concurrency] Add async properties Task.value and Task.result
(cherry picked from commit 7d2ce77)
1 parent 68522f1 commit 44000fd

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

stdlib/public/Concurrency/SourceCompatibilityShims.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,30 @@ extension Task where Success == Never, Failure == Never {
164164
}
165165
}
166166

167+
@available(SwiftStdlib 5.5, *)
168+
extension Task {
169+
@available(*, deprecated, message: "get() has been replaced by .value")
170+
@_alwaysEmitIntoClient
171+
public func get() async throws -> Success {
172+
return try await value
173+
}
174+
175+
@available(*, deprecated, message: "getResult() has been replaced by .result")
176+
@_alwaysEmitIntoClient
177+
public func getResult() async -> Result<Success, Failure> {
178+
return await result
179+
}
180+
}
181+
182+
@available(SwiftStdlib 5.5, *)
183+
extension Task where Failure == Never {
184+
@available(*, deprecated, message: "get() has been replaced by .value")
185+
@_alwaysEmitIntoClient
186+
public func get() async -> Success {
187+
return await value
188+
}
189+
}
190+
167191
@available(SwiftStdlib 5.5, *)
168192
extension TaskGroup {
169193
@available(*, deprecated, renamed: "async(priority:operation:)")

stdlib/public/Concurrency/Task.swift

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,27 @@ extension Task {
8787
/// If the task gets cancelled internally, e.g. by checking for cancellation
8888
/// and throwing a specific error or using `checkCancellation` the error
8989
/// thrown out of the task will be re-thrown here.
90-
public func get() async throws -> Success {
91-
return try await _taskFutureGetThrowing(_task)
90+
public var value: Success {
91+
get async throws {
92+
return try await _taskFutureGetThrowing(_task)
93+
}
9294
}
9395

96+
/// Wait for the task to complete, returning (or throwing) its result.
97+
///
98+
/// ### Priority
99+
/// If the task has not completed yet, its priority will be elevated to the
100+
/// priority of the current task. Note that this may not be as effective as
101+
/// creating the task with the "right" priority to in the first place.
102+
///
103+
/// ### Cancellation
104+
/// If the awaited on task gets cancelled externally the `get()` will throw
105+
/// a cancellation error.
106+
///
107+
/// If the task gets cancelled internally, e.g. by checking for cancellation
108+
/// and throwing a specific error or using `checkCancellation` the error
109+
/// thrown out of the task will be re-thrown here.
110+
94111
/// Wait for the task to complete, returning its `Result`.
95112
///
96113
/// ### Priority
@@ -105,11 +122,13 @@ extension Task {
105122
/// If the task gets cancelled internally, e.g. by checking for cancellation
106123
/// and throwing a specific error or using `checkCancellation` the error
107124
/// thrown out of the task will be re-thrown here.
108-
public func getResult() async -> Result<Success, Failure> {
109-
do {
110-
return .success(try await get())
111-
} catch {
112-
return .failure(error as! Failure) // as!-safe, guaranteed to be Failure
125+
public var result: Result<Success, Failure> {
126+
get async {
127+
do {
128+
return .success(try await get())
129+
} catch {
130+
return .failure(error as! Failure) // as!-safe, guaranteed to be Failure
131+
}
113132
}
114133
}
115134

@@ -142,8 +161,10 @@ extension Task where Failure == Never {
142161
/// way than throwing a `CancellationError`, e.g. it could provide a neutral
143162
/// value of the `Success` type, or encode that cancellation has occurred in
144163
/// that type itself.
145-
public func get() async -> Success {
146-
return await _taskFutureGet(_task)
164+
public var value: Success {
165+
get async {
166+
return await _taskFutureGet(_task)
167+
}
147168
}
148169
}
149170

test/Concurrency/async_tasks.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func test_detached() async throws {
9595
await someAsyncFunc() // able to call async functions
9696
}
9797

98-
let result: String = await handle.get()
98+
let result: String = await handle.value
9999
_ = result
100100
}
101101

@@ -106,7 +106,7 @@ func test_detached_throwing() async -> String {
106106
}
107107

108108
do {
109-
return try await handle.get()
109+
return try await handle.value
110110
} catch {
111111
print("caught: \(error)")
112112
}

0 commit comments

Comments
 (0)