Async Await

Async and await functionality is implemented as an extention on TaskManager.shared.

See async(_:completion:) See await(timeout:block:)

  • Calls an asynchronous function and waits for the result. The function that is passed in must have a completion callback, and this is assumed to be the callback that is called when the asynchronous function is done computing the result, e.g.:

    let f = { (done: @escaping (Int) -> Void) -> Void in
        DispatchQueue.global(qos: .unspecified).async {
            // Do some long running calculation
            done(5)
        }
    }
    
    XCTAssertEqual(try await(f), 5)
    

    Declaration

    Swift

    public func await<T>(timeout: DispatchTimeInterval? = nil, block: @escaping (@escaping (T) -> Void) -> Void) throws -> T

    Parameters

    timeout

    how long to wait or this function to finish

    block

    an asynchronous function that has a done callback as it’s only parameter. The done callback must take the expected value of the asynchronous operation as its only parameter

  • Calls an asynchronous function and waits for completion. The function that is passed in must have a completion callback, and this is assumed to be the callback that is called when the asynchronous function is done.

    Declaration

    Swift

    public func await(timeout: DispatchTimeInterval? = nil, block: @escaping (() -> Void) -> Void) throws

    Parameters

    block

    an asynchronous function that has a done callback as it’s only parameter. The done callback must be called when the asynchronous operation completes.

    timeout

    how long to wait or this function to finish

  • Calls an asynchronous function and waits for the result. The function that is passed in must have a completion callback, and this is assumed to be the callback that is called when the asynchronous function is done computing the result

    Declaration

    Swift

    public func await<T>(timeout: DispatchTimeInterval? = nil, block: @escaping (@escaping (Result<T, Error>) -> Void) -> Void) throws -> T

    Parameters

    block

    an asynchronous function that has a done callback as it’s only parameter. The done callback must take the expected Result<T, Error> of the asynchronous operation as its only parameter

    timeout

    how long to wait or this function to finish

  • Curry a function. This is very useful when using async/await on functions that take more than one parameter and end in a callback

    Declaration

    Swift

    public func curry<A, B, C>(_ f: @escaping (A, B) -> C) -> (A) -> ((B) -> C)

    Parameters

    f

    the function that takes two parameters

    Return Value

    A function object that takes the 1st parameter and returns a function object that takes the 2nd parameter

  • Curry a function. This is very useful when using async/await on functions that take more than one parameter and end in a callback

    Declaration

    Swift

    public func curry<A, B, C, D>(_ f: @escaping (A, B, C) -> D) -> (A, B) -> ((C) -> D)

    Parameters

    f

    the function that takes three parameters

    Return Value

    A function object that takes the 1st and 2nd parameters and returns a function object that takes the 3rd parameter