promisecommandqueue
TypeScript icon, indicating that this package has built-in type declarations

0.1.6 • Public • Published

promise-command-queue

Build Status Coverage Status

A fairly simple command queue that ensures both commands with sync code and commands with async code are executed one by one in sequence, through promises chaining. The idea is to:

  • Ensure errors can always be captured and handled properly whenever they occur.
  • Encapsulate business logic associated in commands which can be easily changed and tested.

How to use

Installation

npm install promisecommandqueue --save

Dispatch tasks

import { ICommand, CommandQueue } from "promisecommandqueue";

const commandQueue = new CommandQueue();

const syncCommand = {
    ID: "SYNC_COMMAND",
    run: () => {
        // Do something ...
    }
};

const asyncCommand = {
    ID: "ASYNC_COMMAND",
    run: () => new Promise<void>((resolve, reject) => {
        // Do something ...
    })
};

commandQueue.dispatch(asyncCommand);
commandQueue.dispatch(syncCommand);
await commandQueue.finish();

Custom error handling

// Enable fail fast, clear the queue whenever an error occurs
const commandQueue = new CommandQueue(true);
// ...
const asyncCommand = {
    ID: "ASYNC_COMMAND",
    run: () => new Promise<void>((resolve, reject) => {
        // Do something ...
    }),
    // Use a custom error handler
    errorHandler: e => {
        console.error(e);
        // Remove commands with the given command ID when an error occurs during the execution of this command
        commandQueue.remove("commandToBeRemoved");
    }
};
commandQueue.dispatch(asyncCommand);
await commandQueue.finish();

Readme

Keywords

none

Package Sidebar

Install

npm i promisecommandqueue

Weekly Downloads

0

Version

0.1.6

License

MIT

Unpacked Size

9.47 kB

Total Files

5

Last publish

Collaborators

  • oscaryu