Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

YSZhuoyang/promise-command-queue

Repository files navigation

Build StatusCoverage 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 occursconst 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

[8]ページ先頭

©2009-2025 Movatter.jp