Coroutine

public struct Coroutine

Additional struct with utility methods to work with coroutines.

Important

All methods must be called inside a coroutine, otherwise CoroutineError.mustBeCalledInsideCoroutine will be thrown.
  • Returns true if this property is called inside a coroutine.

    Declaration

    Swift

    @inlinable
    public static var isInsideCoroutine: Bool { get }

await

  • Suspends a coroutine поки не буде викликаний callback.

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

    Throws

    CoroutineError.mustBeCalledInsideCoroutine якщо метод був викликаний за межами коротини.

    Declaration

    Swift

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

    Parameters

    callback

    The callback для 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.mustBeCalledInsideCoroutine if the method is called outside a coroutine.

    Declaration

    Swift

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

    Parameters

    callback

    The callback for resuming a coroutine.

    Return Value

    The result which is passed to callback.

  • Suspends a coroutine and resumes it on callback.

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

    Throws

    CoroutineError.mustBeCalledInsideCoroutine if the method is called outside a coroutine.

    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.mustBeCalledInsideCoroutine if the method is called outside a coroutine.

    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.

    Return Value

    The result which is passed to callback.

delay

  • Suspends a coroutine for a certain time.

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

    Throws

    CoroutineError.mustBeCalledInsideCoroutine якщо метод був викликаний за межами коротини.

    Declaration

    Swift

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

    Parameters

    time

    The time interval for which a coroutine will be suspended.