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
/// An error indicating that the deadline has passed and the operation did not complete.
7
7
publicstructDeadlineExceededError:Error{}
8
8
9
-
#if swift(>=6.0)
10
9
/// Race the given operation against a deadline.
11
10
///
12
11
/// 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.
/// 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.
210
-
///
211
-
/// - Parameters:
212
-
/// - instant: The absolute deadline for the operation to complete.
213
-
/// - tolerance: The allowed tolerance for the deadline.
214
-
/// - clock: The clock used for timing the operation.
215
-
/// - operation: The asynchronous operation to be executed.
216
-
///
217
-
/// - Returns: The result of the operation if it completes before the deadline.
218
-
/// - Throws: `DeadlineExceededError`, if the operation fails to complete before the deadline and errors thrown by the operation or clock.
219
-
///
220
-
/// ## Examples
221
-
/// To fully understand this, let's illustrate the 3 outcomes of this function:
222
-
///
223
-
/// ### Outcome 1
224
-
/// The operation finishes in time:
225
-
/// ```swift
226
-
/// let result = try await deadline(until: .now + .seconds(5)) {
227
-
/// // Simulate long running task
228
-
/// try await Task.sleep(for: .seconds(1))
229
-
/// return "success"
230
-
/// }
231
-
/// ```
232
-
/// As you'd expect, result will be "success". The same applies when your operation fails in time:
233
-
/// ```swift
234
-
/// let result = try await deadline(until: .now + .seconds(5)) {
235
-
/// // Simulate long running task
236
-
/// try await Task.sleep(for: .seconds(1))
237
-
/// throw CustomError()
238
-
/// }
239
-
/// ```
240
-
/// This will throw `CustomError`.
241
-
///
242
-
/// ## Outcome 2
243
-
/// The operation does not finish in time:
244
-
/// ```swift
245
-
/// let result = try await deadline(until: .now + .seconds(1)) {
246
-
/// // Simulate even longer running task
247
-
/// try await Task.sleep(for: .seconds(5))
248
-
/// return "success"
249
-
/// }
250
-
/// ```
251
-
/// This will throw `DeadlineExceededError` because the operation will not finish in time.
/// 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.
331
-
/// `ContinuousClock` will be used as the default clock.
332
-
///
333
-
/// - Parameters:
334
-
/// - instant: The absolute deadline for the operation to complete.
335
-
/// - tolerance: The allowed tolerance for the deadline.
336
-
/// - operation: The asynchronous operation to be executed.
337
-
///
338
-
/// - Returns: The result of the operation if it completes before the deadline.
339
-
/// - Throws: `DeadlineExceededError`, if the operation fails to complete before the deadline and errors thrown by the operation or clock.
340
-
///
341
-
/// ## Examples
342
-
/// To fully understand this, let's illustrate the 3 outcomes of this function:
343
-
///
344
-
/// ### Outcome 1
345
-
/// The operation finishes in time:
346
-
/// ```swift
347
-
/// let result = try await deadline(until: .now + .seconds(5)) {
348
-
/// // Simulate long running task
349
-
/// try await Task.sleep(for: .seconds(1))
350
-
/// return "success"
351
-
/// }
352
-
/// ```
353
-
/// As you'd expect, result will be "success". The same applies when your operation fails in time:
354
-
/// ```swift
355
-
/// let result = try await deadline(until: .now + .seconds(5)) {
356
-
/// // Simulate long running task
357
-
/// try await Task.sleep(for: .seconds(1))
358
-
/// throw CustomError()
359
-
/// }
360
-
/// ```
361
-
/// This will throw `CustomError`.
362
-
///
363
-
/// ## Outcome 2
364
-
/// The operation does not finish in time:
365
-
/// ```swift
366
-
/// let result = try await deadline(until: .now + .seconds(1)) {
367
-
/// // Simulate even longer running task
368
-
/// try await Task.sleep(for: .seconds(5))
369
-
/// return "success"
370
-
/// }
371
-
/// ```
372
-
/// This will throw `DeadlineExceededError` because the operation will not finish in time.
0 commit comments