1.0.0 • Published 1 year ago

@bs-solutions/async-task-manager v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

async-task-manager

This package is an asynchronous task manager for organizing the sequential execution of asynchronous tasks.

Documentation

Install

This generator requires node and npm.

You can install it by running:

npm install @bs-solutions/async-task-manager

Usage

First you need to create a new instance of the class AsyncTaskQueue (with parameter debug: true if you want to see logs with task execution results in the console):

const asyncQueue = new AsyncTasksQueue({ debug: true });

Then you can use the following methods:

  • asyncQueue.enqueue() method adds an asynchronous task to the queue. This method has two parameters: required parameter queueTask and optional parameter queueTaskConfig. queueTaskConfig type config looks like this:
    type QueueTaskConfig = { 
       resolve?: (args?: unknown) => unknown | Promise<unknown>,
       reject?: (args?: unknown) => unknown | Promise<unknown>,
       clearIfReject?: boolean,
       retry?: {
         count: number,
         delay: number,
       },
    };
    You can see an example below:
    asyncQueue.enqueue(
      () => someAsyncFunc(),  // the task you want to add to the queue
      {
        resolve: (result) => callbackFunc(result), // will be executed if tasks resolve
        reject: (result) => callbackFunc(result), // will be executed if tasks reject after last retry
        clearIfReject: true, // clears all tasks from queue if tasks reject 
        retry: {
          count: 3, // determines the number of attempts
          delay: 2500, // defines the delay between attempts
        },
      },
    );
  • asyncQueue.clearQueue() method removes all tasks from the queue.

Chaining

asyncQueue.enqueue() method always returns an instance of the asyncQueue class, so chaining is supported, and you can use it like this:

asyncQueue
  .enqueue(() => func())
  .enqueue(() => func2())
  .clearQueue();

Maintainer

@VladSolyony

Contributing

Please contribute! Look at the issues.

License

MIT © 2024