DelayAsyncSequence

public struct DelayAsyncSequence<T> : AsyncSequence where T : AsyncSequence
extension DelayAsyncSequence: AsyncIteratorProtocol

Delays emission of all elements by the provided interval.

let stream = AsyncStream<Int> { continuation in
    continuation.yield(0)
    continuation.yield(1)
    continuation.yield(2)
    continuation.finish()
}

let start = Date.now
for element in try await self.stream.delay(for: 0.5) {
    print("\(element) - \(Date.now.timeIntervalSince(start))")
}

// Prints:
// 0 - 0.5
// 1 - 1.0
// 2 - 1.5
  • The kind of elements streamed.

    Declaration

    Swift

    public typealias Element = T.Element

Initialization

  • Creates an async sequence that delays emission of elements and completion.

    Declaration

    Swift

    public init(
        _ base: T,
        interval: TimeInterval
    )

    Parameters

    base

    The async sequence in which this sequence receives it’s elements.

    interval

    The amount of time the async sequence should wait before emitting an element.

AsyncSequence

  • Creates an async iterator that emits elements of this async sequence.

    Declaration

    Swift

    public func makeAsyncIterator() -> DelayAsyncSequence<T>

    Return Value

    An instance that conforms to AsyncIteratorProtocol.

AsyncIteratorProtocol

  • next() Asynchronous

    Produces the next element in the sequence.

    Declaration

    Swift

    public mutating func next() async throws -> Element?

    Return Value

    The next element or nil if the end of the sequence is reached.