Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLeif authored Jun 2, 2023
1 parent 945d124 commit ac9dd51
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,28 @@ Waiter is a Swift library that provides convenient global functions for asynchro
import Waiter

class Counter {
var value: Int = 0
var value: Int = 0
}

let counter = Counter()

Task {
// Asynchronously increment the counter after a delay
await Task.sleep(1)
counter.value += 1
// 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 wait(
on: counter,
for: \.value,
expecting: 1
)
print("Counter value: \(finalValue)") // Output: Counter value: 1
// Wait for the counter value to become equal to 1
let finalValue = try await wait(
on: counter,
for: \.value,
expecting: 1
)

print("Counter value: \(finalValue)") // Output: Counter value: 1
} catch {
print("Timeout Error: \(error)")
print("Timeout Error: \(error)")
}
```

Expand All @@ -43,11 +44,11 @@ To wait for a value of a specified key path to satisfy a condition, use the glob
```swift
@discardableResult
public func wait<Object: AnyObject, Value>(
on object: Object,
for keyPath: KeyPath<Object, Value>,
duration: TimeInterval = 3,
interval: TimeInterval = 0.1,
expecting: @escaping (Value) -> Bool
on object: Object,
for keyPath: KeyPath<Object, Value>,
duration: TimeInterval = 3,
interval: TimeInterval = 0.1,
expecting: @escaping (Value) -> Bool
) async throws -> Value
```

Expand Down Expand Up @@ -92,19 +93,19 @@ import XCTest
@testable import YourProject

final class YourTests: XCTestCase, Waitable {
func testYourFunctionality() async throws {
// Test setup
// Perform asynchronous operations
// Wait for a value to satisfy a condition
try await wait(on: object, for: keyPath, expecting: expectedValue)
// Assertions and test verification
}
func testYourFunctionality() async throws {
// Test setup

// Perform asynchronous operations

// Wait for a value to satisfy a condition
try await wait(on: object, for: keyPath, expecting: expectedValue)

// Assertions and test verification
}
}
```

### Error Handling

The `wait` function can throw errors if the wait times out. If an error occurs, it means the wait duration exceeded the specified timeout and the condition was not satisfied within the given time frame. You can handle the error by displaying an appropriate message, retrying the operation, or taking any other desired action.
The `wait` function can throw errors if the wait times out. If an error occurs, it means the wait duration exceeded the specified timeout and the condition was not satisfied within the given time frame. You can handle the error by displaying an appropriate message, retrying the operation, or taking any other desired action.

0 comments on commit ac9dd51

Please sign in to comment.