npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

a-small-promise-queue

v1.0.9

Published

a small promise queue

Readme

a-small-promise-queue

QQ Gitee GitHub NPM

A simple queue for better control of your asynchronous events.

Setup

npm install --save a-small-promise-queue

Usage

You can then use it as a window global or as an CommonJs module

Node

// for commonjs
const QueueTool = require('a-small-promise-queue')
QueueTool.PromiseQueue

// for esModule
import PromiseQueue from 'a-small-promise-queue'

Browser

<script src="./lib/a-small-promise-queue.umd.js"></script>
QueueTool.PromiseQueue

Example

import PromiseQueue from 'a-small-promise-queue'

const queue = new PromiseQueue({
    maxConcurrenceCount: 2, // 最大并发数设置为2
    successCallBack: (result) => {
        console.log('执行完成', result)
    }
})

function test(index, time) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(index)
        }, time)
    })
}

queue.add(() => {
    return test(1, 2000) // 输入Promise函数
})

queue.add(async () => {
    await test(2, 2000) // 使用async await
    return 2
})

queue.add(() => {
    return test(3, 2000)
})

queue.add(() => {
    return test(4, 2000)
})

queue.add(() => {
    return test(5, 2000)
})

queue.add(() => {
    return test(6, 2000)
})

queue.add(() => {
    console.log('开始执行', 7) // 输入普通函数
})

queue.add((opt: PQCallbackOptions) => { // 自动在函数最后一个参数追加一个回调函数对象,可以用于中断异步函数的执行
    return '开始执行8' + JSON.stringify(opt)
})

queue.add(() => {
    return test(9, 2000)
})

queue.add(test.bind(null, 10, 2000)) // 使用bind方法

Interface

type TaskState = "ready" | "running" | "pause" | "clear";

interface PQOptions {
  /**
   * 是否跳过错误(默认true)
   */
  isSkipError?: boolean;
  /**
   * 是否在添加时自动执行(默认true)
   */
  isAutoExecByAdd?: boolean;
  /**
   * 超时时长(ms)(默认为-1,不限制)
   */
  timeout?: number;
  /**
   * 最大队列长度(默认为-1,不限制)
   */
  maxSize?: number;
  /**
   * 最大并发数(默认为1)
   */
  maxConcurrenceCount?: number;
  /**
   * 执行成功的回调函数
   * @param result 传入异步方法的返回值
   * @returns
   */
  successCallBack?: (result: any) => void;
  /**
   * 执行错误时的回调函数
   * @param error
   * @returns
   */
  errorCallBack?: (error: any) => void;
  /**
   * 任务列表执行完成时的返回值
   * @returns
   */
  taskEndCallBack?: () => void;
}

interface PQItemOptions {
  /**
   * 是否跳过错误
   */
  isSkipError?: boolean;
  /**
   * 超时时长(ms)
   */
  timeout?: number;
  /**
   * 执行成功的回调函数
   * @param result 传入异步方法的返回值
   * @returns
   */
  successCallBack?: (result: any) => void;
  /**
   * 执行错误时的回调函数
   * @param error
   * @returns
   */
  errorCallBack?: (error: any) => void;
}

interface PQCallbackOptions {
  /**
   * 是否超时
   */
  isTimeout: boolean;
  /**
   * 是否处于暂停状态
   */
  isPaused: boolean;
  /**
   * 调度器的状态(ready就绪状态、running执行状态、pause暂停状态、clear清除状态)
   */
  schedulerState: TaskState;
}

class PromiseQueue {
  constructor(options?: PQOptions);
  /**
   * 当前列表中的任务数
   */
  size: number;
  /**
   * 列表中的最大任务数
   */
  maxSize: number;
  /**
   * 当前正在执行的任务数
   */
  runningSize: number;
  /**
   * 添加任务到队列中
   * @param asyncEvent 在执行中会自动在函数的最后追加一个参数,用于传递当前的执行状态
   * @param opt 单独任务的配置项
   */
  add(asyncEvent: (...args: any[]) => Promise<any>, opt?: PQItemOptions): void;
  /**
   * 开始执行
   */
  start(): void;
  /**
   * 暂停执行
   */
  pause(): void;
  /**
   * 清除任务队列
   */
  clear(): void;
}