TKMAsync

public class TKMAsync

Undocumented

  • Creates a completed promise that resolves to the given value.

    Declaration

    Swift

    public static func promiseFromResult<T>(_ result: T?) -> TKMPromise<T>

    Parameters

    result

    the value the promise resolves to.

    Return Value

    a promise that is completed and resolves to the given value.

  • Creates a failed promise, that resolves to the given error.

    Declaration

    Swift

    public static func promiseFromError<T>(_ e: Error) -> TKMPromise<T>

    Parameters

    e

    the error the promise resolves to.

    Return Value

    a promise that is in a failed state and resolves to the given error.

  • Creates a completed promise that resolves to the given TKMAsyncResult.

    Declaration

    Swift

    public static func promiseFromAsyncResult<T>(_ asyncResult: (T?, Error?)) -> TKMPromise<T>

    Parameters

    asyncResult

    the result the promise resolves to.

    Return Value

    a promise that is completed and resolves to the given result.

  • Runs the specified operation on a multi-threaded worker-thread-backed scheduler. If the specified operation completes successfully, the returned TKMPromise<T> resolves to the returned value. Otherwise it will resolve to the raised Error.

    Declaration

    Swift

    public static func executeAsync<T>(_ f: @escaping () throws -> T?) -> TKMPromise<T>

    Parameters

    f

    async operation to run.

    Return Value

    TKMPromise<T> that continues on the caller’s current TKMAsyncScheduler with the operation’s result.

  • Executes the specified function on the specified scheduler and returns a promise which will deliver the result on the caller’s current TKMAsyncScheduler.

    Declaration

    Swift

    public static func executeOnScheduler<T>(scheduler: TKMAsyncScheduler, _ f: @escaping () throws -> T?) -> TKMPromise<T>

    Parameters

    scheduler

    scheduler to run the given operation on.

    f

    operation to run.

    Return Value

    TKMPromise<T> that continues on the caller’s current TKMAsyncScheduler with the operation’s result.

  • Returns a TKMPromise<T> that completes after the given time span.

    Declaration

    Swift

    public static func delayAsync(delayMs: Int64) -> TKMPromise<Void>

    Parameters

    delayMs

    the number of milliseconds to delay.

    Return Value

    a TKMPromise<T> that completes after the given time span.

  • Returns a TKMPromise<T> that completes after either the given time span elapsed or the given TKMCancellationToken requested cancellation. In the first case, the returned TKMPromise<T> completes successfully while in the second case it completes exceptionally with an TKMRuntimeError.cancellationError.

    Declaration

    Swift

    public static func delayAsync(delayMs: Int64, cancellationToken: TKMCancellationToken) -> TKMPromise<Void>

    Parameters

    delayMs

    the number of milliseconds to delay.

    cancellationToken

    the TKMCancellationToken that can be used for cancellation.

    Return Value

    a TKMPromise<T> that completes either after the given time span elapsed or after cancellation, whatever happens earlier.

  • Returns a completed TKMPromise<T> that resolves to null.

    Declaration

    Swift

    public static func first() -> TKMPromise<Void>

    Return Value

    a completed TKMPromise<T> that resolves to null.

  • Synchronously executes the given TKMAction and then returns a completed TKMPromise<T> that resolves to null. If execution of the action fails, the returned promise completes exceptionally and resolves to the raised Error.

    Declaration

    Swift

    public static func first(_ action: @escaping () throws -> Void) -> TKMPromise<Void>

    Parameters

    action

    the action to execute.

    Return Value

    a completed TKMPromise<T> that resolves according to the outcome of the specified action.

  • Synchronously executes the given TKMFunc and then returns a completed TKMPromise<T> that resolves to the value returned by the function. If execution fails, the returned promise completes exceptionally and resolves to the raised Error.

    Declaration

    Swift

    public static func first<T>(_ fn: @escaping () throws -> T?) -> TKMPromise<T>

    Parameters

    fn

    the function to execute.

    Return Value

    a completed TKMPromise<T> that resolves according to the outcome of the specified function.

  • Executes the given TKMFunc and returns the TKMPromise<T> returned by the function. The purpose of this function is to use it for structuring async code. It doesn’t provide any actual functionality.

    Instead of writing code like in this example

         firstAsyncOperation().continueAsyncOnUi { result ->
                 return secondAsyncOperation()
         }.continueAsyncOnUi { result ->
                 return thirdAsyncOperation()
         }.conclude()
    

    we could also write in a more readable way

         TKMAsync.firstAsync { () ->
                 return firstAsyncOperation()
         }.continueAsyncOnUi { result ->
                 return secondAsyncOperation()
         }.continueAsyncOnUi { result ->
                 return thirdAsyncOperation()
         }.conclude()
    

    Declaration

    Swift

    public static func firstAsync<T>(_ f: @escaping () -> TKMPromise<T>) -> TKMPromise<T>

    Parameters

    f

    The async function to execute.

    Return Value

    The Promise returned by the function.

  • Starts an async function on the specified scheduler and continues the returned promise on the caller’s current scheduler.

    Declaration

    Swift

    public static func switchToScheduler<T>(scheduler: TKMAsyncScheduler, _ f: @escaping () -> TKMPromise<T>) -> TKMPromise<T>

    Parameters

    scheduler

    Scheduler to start the specified async operation on.

    f

    Async operation to be started on the specified scheduler.

    Return Value

    Promise that continues on the caller’s current scheduler.

  • Starts an async function on the specified scheduler and continues the returned promise on the other specified scheduler.

    Declaration

    Swift

    public static func switchToScheduler<T>(runOnScheduler: TKMAsyncScheduler, continueOnScheduler: TKMAsyncScheduler, _ f: @escaping () -> TKMPromise<T>) -> TKMPromise<T>

    Parameters

    runOnScheduler

    Scheduler to start the specified async operation on.

    continueOnScheduler

    Scheduler to continue the returned promise on.

    f

    Async operation to be started on the specified scheduler.

    Return Value

    Promise that continues on the specified scheduler.