Skip to content

Commit 7d2ce77

Browse files
committed
[Concurrency] Add async properties Task.value and Task.result
1 parent 72d8197 commit 7d2ce77

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
@@ -60,10 +60,27 @@ extension Task {
6060
/// If the task gets cancelled internally, e.g. by checking for cancellation
6161
/// and throwing a specific error or using `checkCancellation` the error
6262
/// thrown out of the task will be re-thrown here.
63-
public func get() async throws -> Success {
64-
return try await _taskFutureGetThrowing(_task)
63+
public var value: Success {
64+
get async throws {
65+
return try await _taskFutureGetThrowing(_task)
66+
}
6567
}
6668

69+
/// Wait for the task to complete, returning (or throwing) its result.
70+
///
71+
/// ### Priority
72+
/// If the task has not completed yet, its priority will be elevated to the
73+
/// priority of the current task. Note that this may not be as effective as
74+
/// creating the task with the "right" priority to in the first place.
75+
///
76+
/// ### Cancellation
77+
/// If the awaited on task gets cancelled externally the `get()` will throw
78+
/// a cancellation error.
79+
///
80+
/// If the task gets cancelled internally, e.g. by checking for cancellation
81+
/// and throwing a specific error or using `checkCancellation` the error
82+
/// thrown out of the task will be re-thrown here.
83+
6784
/// Wait for the task to complete, returning its `Result`.
6885
///
6986
/// ### Priority
@@ -78,11 +95,13 @@ extension Task {
7895
/// If the task gets cancelled internally, e.g. by checking for cancellation
7996
/// and throwing a specific error or using `checkCancellation` the error
8097
/// thrown out of the task will be re-thrown here.
81-
public func getResult() async -> Result<Success, Failure> {
82-
do {
83-
return .success(try await get())
84-
} catch {
85-
return .failure(error as! Failure) // as!-safe, guaranteed to be Failure
98+
public var result: Result<Success, Failure> {
99+
get async {
100+
do {
101+
return .success(try await get())
102+
} catch {
103+
return .failure(error as! Failure) // as!-safe, guaranteed to be Failure
104+
}
86105
}
87106
}
88107

@@ -114,8 +133,10 @@ extension Task where Failure == Never {
114133
/// way than throwing a `CancellationError`, e.g. it could provide a neutral
115134
/// value of the `Success` type, or encode that cancellation has occurred in
116135
/// that type itself.
117-
public func get() async -> Success {
118-
return await _taskFutureGet(_task)
136+
public var value: Success {
137+
get async {
138+
return await _taskFutureGet(_task)
139+
}
119140
}
120141
}
121142

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)