• Call function repeatedly while not fully blocking the main JS thread. It's useful for execution of loops that can take some time process and we don't want to block the main thread completely (and don't want to start a new thread either).

    Parameters

    • fn: ((idx, token?) => boolean)

      Function to be called with counter as a first parameter and token as a second parameter starting from second parameter of this function. Returns true when all operations are complete or false when new loop is needed.

        • (idx, token?): boolean
        • Parameters

          Returns boolean

    • Optional valFrom: number

      Starting counter value

    • Optional token: AsyncLoopToken

      Token. An object with cancel property to cancel loop

    • Optional finishedCallback: ((idx, token?) => void)

      Function to be called when all operations are done (main function from first parameter returns true) with parameters - counter, token.

        • (idx, token?): void
        • Parameters

          Returns void

    Returns void

    Example

    asyncLoop( function(i) {
    if (i >= arr.length)
    return false;
    else {
    arr[i].processIt;
    }
    });

    Method

    asyncLoop