https://github.com/raykitajima/asynctimeout
A Swift utility provides timeout for async func
https://github.com/raykitajima/asynctimeout
Last synced: 14 days ago
JSON representation
A Swift utility provides timeout for async func
- Host: GitHub
- URL: https://github.com/raykitajima/asynctimeout
- Owner: RayKitajima
- License: mit
- Created: 2023-09-17T05:31:59.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-09-24T07:04:51.000Z (almost 3 years ago)
- Last Synced: 2026-05-01T08:00:05.714Z (2 months ago)
- Language: Swift
- Size: 3.91 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AsyncTimeout
A Swift utility that provides a timeout mechanism for asynchronous operations using the new async/await syntax introduced in Swift 5.5.
## Features:
- Compatibility with iOS 13.0+ and macOS 10.15+
- Uses Swift's new concurrency features such as `actor` and `TaskGroup`
- Throws a `timeout` error if the operation doesn't complete within the specified timeout.
## How to Use:
1. Ensure that you're using Swift 5.5 or later and targeting at least iOS 13.0 or macOS 10.15.
2. Integrate the `AsyncTimeout` structure into your project.
3. Use the static `withTimeout` function to wrap the async operation that you want to time out.
### Swift Package Manager
Add the following to your Package.swift dependencies:
```swift
dependencies: [
.package(url: "https://github.com/RayKitajima/AsyncTimeout.git", from: "1.0.0"),
],
```
### Example:
```swift
import AsyncTimeout
do {
let result: YourReturnType = try await AsyncTimeout.withTimeout(seconds: 5) {
return try await someAsyncFunc()
}
} catch AsyncTimeout.CustomError.timeout {
print("The operation timed out!")
} catch {
print("An error occurred: \(error.localizedDescription)")
}
```
## Details:
### Structs & Enums:
- `AsyncTimeout`: The main struct containing the timeout logic.
- `OperationState`: An actor that tracks if the operation has been completed. It's used to safely handle state across potentially concurrent code.
- `CustomError`: An enum that has a `timeout` case, representing a timeout error.
### Main Function:
- `withTimeout(seconds:operation:)`: This function takes a timeout interval (in seconds) and an async operation. If the operation doesn't complete within the given timeout, it throws a timeout error.
## Notes:
- The utility employs a polling mechanism to check if the operation has completed. The current check interval is set at 0.1 seconds. This might lead to a maximum delay of 0.1 seconds more than the specified timeout.
## Disclaimer:
- The function assumes that the provided async operation will complete successfully, i.e., the result is forcefully unwrapped at the end (`result!`). It's essential to ensure the operation doesn't result in a nil value, or it will crash.