Swift @propertyWrapper that waits asynchronously until the value matches a predicate





Introduction

@Awaiting is an open-source Swift @propertyWrapper that waits asynchronously until the value matches a predicate.

Usage

Any class can declare a property to be @Awaiting as follows:

@Awaiting var isComplete: Bool = false

You then use its projected value to await until some predicate is met;

// Suspends until isComplete == true
_ = try await $isComplete.first(where: { $0 })

Cancellation

CancellationError is thrown if the task is cancelled before the predicate is met.

Optionals

When optionals are wrapped you can wait for the first non nil value:

@Awaiting var name: String?

// Suspends until name != nil
let name = try await $name.first()

Collections

When collections are wrapped you can wait for at least n elements to exist:

@Awaiting var names = [String]()

// Suspends until names.count >= 1
let nonEmpty = try await $names.first(withAtLeast: 1)

Credits

@Awaiting is primarily the work of Simon Whitty.

GitHub

View Github