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
+10-7Lines changed: 10 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,26 +13,29 @@ Since this algorithm is not easy to get right and the implementation in [swift-a
13
13
14
14
The library comes with two free functions, one with a generic clock. And another one which uses the `ContinuousClock` as default.
15
15
```swift
16
-
publicfuncwithDeadline<C, R>(
16
+
publicfuncdeadline<C, R>(
17
17
untilinstant: C.Instant,
18
18
tolerance: C.Instant.Duration?=nil,
19
19
clock: C,
20
+
isolation: isolated (any Actor)?=#isolation,
20
21
operation: @Sendable () asyncthrows-> R
21
22
) asyncthrows-> R where C:Clock, R:Sendable { ... }
22
23
23
-
publicfuncwithDeadline<R>(
24
+
publicfuncdeadline<R>(
24
25
untilinstant: ContinuousClock.Instant,
25
26
tolerance: ContinuousClock.Instant.Duration?=nil,
27
+
isolation: isolated (any Actor)?=#isolation,
26
28
operation: @Sendable () asyncthrows-> R
27
29
) asyncthrows-> R where R:Sendable { ... }
28
30
```
29
31
30
32
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.
31
33
32
34
- Parameters:
33
-
-`until`: The absolute deadline for the operation to complete.
35
+
-`instant`: The absolute deadline for the operation to complete.
34
36
-`tolerance`: The allowed tolerance for the deadline.
35
37
-`clock`: The clock used for timing the operation.
38
+
-`isolation`: The isolation passed on to the task group.
36
39
-`operation`: The asynchronous operation to be executed.
37
40
38
41
- Returns: The result of the operation if it completes before the deadline.
@@ -47,15 +50,15 @@ To fully understand this, let's illustrate the 3 outcomes of this function:
47
50
#### Outcome 1
48
51
The operation finishes in time:
49
52
```swift
50
-
let result =tryawaitwithDeadline(until: .now+ .seconds(5)) {
53
+
let result =tryawaitdeadline(until: .now+ .seconds(5)) {
51
54
// Simulate long running task
52
55
tryawait Task.sleep(for: .seconds(1))
53
56
return"success"
54
57
}
55
58
```
56
59
As you'd expect, result will be "success". The same applies when your operation fails in time:
57
60
```swift
58
-
let result =tryawaitwithDeadline(until: .now+ .seconds(5)) {
61
+
let result =tryawaitdeadline(until: .now+ .seconds(5)) {
59
62
// Simulate long running task
60
63
tryawait Task.sleep(for: .seconds(1))
61
64
throwCustomError()
@@ -66,7 +69,7 @@ This will throw `CustomError`.
66
69
#### Outcome 2
67
70
The operation does not finish in time:
68
71
```swift
69
-
let result =tryawaitwithDeadline(until: .now+ .seconds(1)) {
72
+
let result =tryawaitdeadline(until: .now+ .seconds(1)) {
0 commit comments