Function createTaskScheduler

  • Create a Task Scheduler using the optional promise implementation and scheduler name. The newPromise can be any value promise creation function, where the execution of the queued tasks will be processed based on how the promise implementation processes it's chained promises (asynchrounsly; synchronously; idle processing, etc)

    The functions used to start each task my return a result (synchronous execution) or an IPromise, PromiseLike or Promise result (asynchronous execution).

    Each task is executed in the order that it was queued and the provided startTask function will not be called until all previous tasks have completed (whther they resolve or reject). The result from any previous task does not affect and is not passed to any later scheduled task, if you need this capability then your startTask functions will need to co-operate to share any common context.

    By default, queued tasks which have either been "waiting" to run or have been running longer then 10 minutes will be Auto-Rejected to try and free up resources. If a task is running when it rejected then it will continue to "run" based on whatever operation it's startTask is performing. If a task has not yet had it's startTask function called it will never get called. In both cases the IPromise returned by the call to queue the task will be rejected. You can change this default time, including disabling completly via the setStaleTimeout function.

    Parameters

    • OptionalnewPromise: (<T>(executor: PromiseExecutor<T>, timeout?: number) => IPromise<T>)

      The function to use for creating a new promise when required, if not provided this will default to createPromise which will use the default registered promise creation function which defaults to runtime native promises or async Promise if not supported by the runtime.

    • Optionalname: string

      The name you want to associated with this scheduler, mostly useful for debugging

    Returns ITaskScheduler

    A new ITaskScheduler instance

    0.2.0

    let scheduler = createTaskScheduler();

    // Schedule a task using the ts-async helper promise functions
    scheduler.queue(() => {
    return createPromise((resolve, reject) => {
    scheduleTimeout(() => {
    // Do something after a delay
    }, 100);
    });
    });

    // Schedule an asynchronous task which uses async/await
    scheduler.queue(async () => {
    // This task will only execute after the previous task has completed
    await performAnotherAsyncTask();
    });

    // Schedule a synchronous task that executes and completes immediately
    scheduled.queue(() => {
    // Do some synchronous task
    return 42;
    });

    // Schedule an asynchronous task which returns a promise
    scheduled.queue(() => {
    return doAwait(fetch("https://github.com/nevware21/ts-async/blob/main/README.md"), (response) => {
    let theReadMe = response.text();
    // Do something with the readme
    });
    });