Skip to content

Repository files navigation

BuildCodecovPlatformsSwift 6.0

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.3.0"))

Usage

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

import Timeout
letval=tryawaitwithThrowingTimeout(after:.now +.seconds(2)){tryawaitperform()}

TimeInterval can also be provided:

letval=tryawaitwithThrowingTimeout(seconds:2.0){tryawaitperform()}

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

An overload includes a TimeoutController object allowing the body to cancel or move the expiration where required:

tryawaitwithThrowingTimeout(seconds:1.0){ timeout intryawaitfoo()
timeout.cancelExpiration()tryawaitbar()
timeout.expire(after:.now +.seconds(0.5))tryawaitbaz()}

AsyncTimeoutSequence allows each iteration a fresh timeout to return the next element;

fortryawaitvalin sequence.timeout(seconds:2.0){...}

Credits

swift-timeout is primarily the work of Simon Whitty.

(Full list of contributors)

About

Swift Concurrency timeout handler

Resources

Stars

75 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages