Skip to content

YSZhuoyang/promise-command-queue

Repository files navigation

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();

About

A fairly simple command queue that ensures both commands containing sync code and commands containing async code are executed in sequence, through promises chaining.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published