You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+59-13Lines changed: 59 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -7,14 +7,6 @@ As I've previously stated on the [Swift forums](https://forums.swift.org/t/my-ex
7
7
Since this algorithm is not easy to get right and the implementation in [swift-async-algorithms](https://github.com/apple/swift-async-algorithms/pull/215) has been laying around without getting merged for quite some time now, I decided to open-source my implementation.
8
8
9
9
## Details
10
-
It will start a `TaskGroup` with two child tasks: your operation and a `Task.sleep(until:tolerance:clock:)`. There are three possible outcomes:
11
-
1. If your operation finishes first, it will simply return the result and cancel the sleeping.
12
-
2. If the sleeping finishes first, it will throw a `DeadlineExceededError` and cancel your operation.
13
-
3. If the parent task was cancelled, it will automatically cancel your operation and the sleeping. The cancellation handling will be inferred from your operation. `CancellationError`s from `Task.sleep(until:tolerance:clock:)` will be ignored.
14
-
15
-
> [!CAUTION]
16
-
> The operation closure must support cooperative cancellation.
17
-
> Otherwise, `withDeadline(until:tolerance:clock:operation:)` will suspend execution until the operation completes, making the deadline ineffective.
18
10
19
11
The library comes with two free functions, one with a generic clock. And another one which uses the `ContinuousClock` as default.
20
12
```swift
@@ -32,12 +24,66 @@ public func withDeadline<T>(
32
24
) asyncthrows-> T where T:Sendable { ... }
33
25
```
34
26
35
-
## Example
36
-
This is just a demonstrative usage of this function. `CBCentralManager.connect(_:)` is a good example, in my opinion, since it does not support timeouts natively.
27
+
This function provides a mechanism for enforcing timeouts on asynchronous operations that lack native deadline support. It creates a `TaskGroup` with two concurrent tasks: the provided operation and a sleep task.
28
+
29
+
- Parameters:
30
+
-`until`: The absolute deadline for the operation to complete.
31
+
-`tolerance`: The allowed tolerance for the deadline.
32
+
-`clock`: The clock used for timing the operation.
33
+
-`operation`: The asynchronous operation to be executed.
34
+
35
+
- Returns: The result of the operation if it completes before the deadline.
36
+
- Throws: `DeadlineExceededError`, if the operation fails to complete before the deadline and errors thrown by the operation itself.
37
+
38
+
> [!CAUTION]
39
+
> The operation closure must support cooperative cancellation. Otherwise, the deadline will not be respected.
40
+
41
+
### Examples
42
+
To fully understand this, let's illustrate the 3 outcomes of this function:
37
43
38
-
Again, if you try to make something like `CBCentralManager.connect(_:)` asynchronous and use it with `withDeadline(until:tolerance:clock:operation:)` be sure to use `withTaskCancellationHandler(operation:onCancel:)` at some point to opt into cooperative cancellation.
44
+
#### Outcome 1
45
+
The operation finishes in time:
46
+
```swift
47
+
let result =tryawaitwithDeadline(until: .now+ .seconds(5)) {
48
+
// Simulate long running task
49
+
tryawait Task.sleep(for: .seconds(1))
50
+
return"success"
51
+
}
52
+
```
53
+
As you'd expect, result will be "success". The same applies when your operation fails in time:
0 commit comments