Class Task<T, R>

Task class represents an individual task which in very simplest form calls its defined worker function with its defined worker parameters on execution.

Remarks

It can optionally have a worker function and worker parameters in its configuration. It has an execute method which is used to run the task and store its result.

If any of worker function or worker parameters is undefined then it uses the fallback configuration for the execution.

It can be a sub task of a group task.

Example

Task with no configuration.

const task = Task();
// or
const task = Task({});

Example

Task with worker function only.

const task = Task<[number, number], number>({
worker: (a, b) => a + b,
});

Example

Task with worker parameters only.

const task = Task<[number, number], unknown>({
workerParams: [1, 2],
});

Example

Task with both worker function and woker parameters.

const task = Task<[number, number], number>({
worker: (a, b) => a + b,
workerParams: [1, 2],
});

Type Parameters

  • T extends unknown[]

    The type of the task worker parameters.

  • R

    The type of the task worker result.

Hierarchy

Constructors

Accessors

Methods

Constructors

  • Type Parameters

    • T extends unknown[]

      The type of the task worker parameters.

    • R

      The type of the task worker result.

    Parameters

    • taskConfig: TaskConfigI<T, R> = {}

      The configuration options for the task.

    Returns Task<T, R>

    Default Value

    {}
    

Accessors

Methods

  • Executes the task.

    Parameters

    • fbConfig: TaskConfigI<T, R> = {}

      The fallback task configuration.

    Returns TaskResultT<R>

    The task worker result.

    Remarks

    A task must have a worker function and worker parameters defined at the time of execution.

    If any of them is undefined, then it should be set or provided via the fallback configuration to use for the execution, else it will throw an error.

    Fallback configuration does not set the worker function or worker parameters but only provides the temporary values to fallback on.

    If even fallback values are undefined, then it throws an error.

    Throws

    If no configuration and no fallback configuration is set.

    Throws

    If no worker function is set and no fallback worker function is provided either.

    Throws

    If no worker parameters are set and no fallback worker parameters are provided either.

    Example

    Without fallback configuration.

    task.execute();
    // will run successfully if task.worker and task.workerParams are not undefined

    Example

    With a fallback worker function.

    task.execute({ worker: (a, b) => a - b });
    // fallback worker function will be used only if task.worker is undefined
    // will run successfully if task.workerParams is not undefined

    Example

    With fallback worker parameters.

    task.execute({ workerParams: [3, 4] });
    // fallback worker parameters will be used only if task.workerParams is undefined
    // will run successfully if task.worker is not undefined

    Example

    With both fallback worker and worker parameters.

    task.execute({ worker: (a, b) => a - b, workerParams: [3, 4] });
    // fallback worker function will be used only if task.worker is undefined
    // fallback worker parameters will be used only if task.workerParams is undefined
    // will run successfully even if task.worker and task.workerParams are undefined

Generated using TypeDoc