forked from sideeffect-io/AsyncExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncFailSequence.swift
49 lines (41 loc) · 1.2 KB
/
AsyncFailSequence.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// AsyncFailSequence.swift
//
//
// Created by Thibault Wittemberg on 04/01/2022.
//
/// `AsyncFailSequence` is an AsyncSequence that outputs no elements and throws an error.
/// If the parent task is cancelled while iterating then the iteration finishes before emitting the error.
///
/// ```
/// let failSequence = AsyncFailSequence<Int, Swift.Error>(NSError(domain: "", code: 1))
/// do {
/// for try await element in failSequence {
/// // will never be called
/// }
/// } catch {
/// // will catch `NSError(domain: "", code: 1)` here
/// }
/// ```
public struct AsyncFailSequence<Element>: AsyncSequence, Sendable {
public typealias Element = Element
public typealias AsyncIterator = Iterator
let error: Error
public init<Failure: Error>(_ error: Failure) {
self.error = error
}
public func makeAsyncIterator() -> AsyncIterator {
Iterator(error: self.error)
}
public struct Iterator: AsyncIteratorProtocol, Sendable {
var error: Error?
public mutating func next() async throws -> Element? {
guard !Task.isCancelled else { return nil }
guard let error = self.error else {
return nil
}
self.error = nil
throw error
}
}
}