Skip to content

Latest commit

 

History

History
49 lines (33 loc) · 2.07 KB

README.md

File metadata and controls

49 lines (33 loc) · 2.07 KB

Build Codecov Platforms Swift 6.0 License Twitter

Introduction

swift-timeout is a lightweight wrapper around Task that executes a closure with a given timeout.

Installation

Timeout can be installed by using Swift Package Manager.

Note: Timeout requires Swift 5.10 on Xcode 15.4+. It runs on iOS 13+, tvOS 13+, macOS 10.15+, Linux and Windows. To install using Swift Package Manager, add this to the dependencies: section in your Package.swift file:

.package(url: "https://github.com/swhitty/swift-timeout.git", .upToNextMajor(from: "0.2.0"))

Usage

Provide a closure and a Instant for when the child task must complete else TimeoutError is thrown:

import Timeout

let val = try await withThrowingTimeout(after: .now + .seconds(2)) {
  try await perform()
}

TimeInterval can also be provided:

let val = try await withThrowingTimeout(seconds: 2.0) {
  try await perform()
}

Note: When the timeout expires the task executing the closure is cancelled and TimeoutError is thrown.

Credits

Timeout is primarily the work of Simon Whitty.

(Full list of contributors)