Coroutine

public struct Coroutine

Additional struct with utility methods to work with coroutines.

Important

All await() methods must be called inside a coroutine.

To check if inside a coroutine, use Coroutine.isInsideCoroutine. If you call await() outside the coroutine, the precondition inside these methods will fail, and you'ill get an error. In -Ounchecked builds, where preconditions are not evaluated to avoid any crashes, a thread-blocking mechanism is used for waiting the result.

  • Returns true if this property is called inside a coroutine.

    func awaitSomeData() throws -> Data {
        //check if inside a coroutine
        guard Coroutine.isInsideCoroutine else { throw . . . some error . . . }
        try Coroutine.await { . . . return some data . . . }
    }
    

    Declaration

    Swift

    @inlinable
    public static var isInsideCoroutine: Bool { get }
  • Starts a new coroutine.

    Declaration

    Swift

    @inlinable
    public static func start(_ task: @escaping () throws -> Void)

    Parameters

    task

    The closure that will be executed inside coroutine.

await

  • Suspends a coroutine and resumes it on callback. Must be called inside a coroutine.

    queue.startCoroutine {
        try Coroutine.await { callback in
            someAsyncFunc { callback() }
        }
    }
    

    Throws

    CoroutineError.

    Declaration

    Swift

    @inlinable
    public static func await(_ callback: (@escaping () -> Void) -> Void) throws

    Parameters

    callback

    The callback to resume coroutine.

  • Suspends a coroutine and resumes it on callback.

    queue.startCoroutine {
        let result = try Coroutine.await { callback in
            someAsyncFunc { result in callback(result) }
        }
    }
    

    Throws

    CoroutineError.

    Declaration

    Swift

    @inlinable
    public static func await<T>(_ callback: (@escaping (T) -> Void) -> Void) throws -> T

    Parameters

    callback

    The callback for resuming a coroutine. Must be called inside a coroutine.

    Return Value

    The result which is passed to callback.

  • Suspends a coroutine and resumes it on callback. Must be called inside a coroutine.

    queue.startCoroutine {
        let (a, b) = try Coroutine.await { callback in
            someAsyncFunc(callback: callback)
        }
    }
    

    Throws

    CoroutineError.

    Declaration

    Swift

    @inlinable
    public static func await<T, N>(_ callback: (@escaping (T, N) -> Void) -> Void) throws -> (T, N)

    Parameters

    callback

    The callback для resume coroutine.

    Return Value

    The result which is passed to callback.

  • Suspends a coroutine and resumes it on callback.

    queue.startCoroutine {
        let (a, b, c) = try Coroutine.await { callback in
            someAsyncFunc(callback: callback)
        }
    }
    

    Throws

    CoroutineError.

    Declaration

    Swift

    @inlinable
    public static func await<T, N, M>(_ callback: (@escaping (T, N, M) -> Void) -> Void) throws -> (T, N, M)

    Parameters

    callback

    The callback для resume coroutine. Must be called inside a coroutine.

    Return Value

    The result which is passed to callback.

delay

  • Suspends a coroutine for a certain time. Must be called inside a coroutine.

    queue.startCoroutine {
        while !someCondition() {
            try Coroutine.delay(.seconds(1))
        }
    }
    

    Throws

    CoroutineError.

    Declaration

    Swift

    @inlinable
    public static func delay(_ time: DispatchTimeInterval) throws

    Parameters

    time

    The time interval for which a coroutine will be suspended.