From 9b45c092d6d73121705573d14919bce17f2352b1 Mon Sep 17 00:00:00 2001 From: Zach Date: Thu, 19 Oct 2023 18:54:03 -0600 Subject: [PATCH] Update README.md --- README.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d821dc9..c46cc36 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Waiter is a Swift library that provides convenient global functions for asynchro ## Usage -### Example +### Wait Example ```swift import Waiter @@ -37,6 +37,33 @@ do { } ``` +### Waitable Example + +```swift +import Waiter + +class Counter: Waitable { + var value: Int = 0 +} + +let counter = Counter() + +Task { + // Asynchronously increment the counter after a delay + await Task.sleep(1) + counter.value += 1 +} + +do { + // Wait for the counter value to become equal to 1 + let finalValue = try await counter.wait(for: \.value, expecting: 1) + + print("Counter value: \(finalValue)") // Output: Counter value: 1 +} catch { + print("Timeout Error: \(error)") +} +``` + ### Wait for a Value to Satisfy a Condition To wait for a value of a specified key path to satisfy a condition, use the global `wait` function: