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

@triyanox/async-sequence

v0.1.8

Published

A library for composing asynchronous operations in a sequence.

Downloads

14

Readme

promise-sequencer

PromiseSequencer is a utility class that enables you to execute promises sequentially with concurrency and retry capabilities. It empowers you to manage promise execution order, concurrency, and handles retries and logging seamlessly.

Installation

Install PromiseSequencer using your preferred package manager:

# Using npm
npm install @triyanox/async-sequence

# Using Bun
bun add @triyanox/async-sequence

# Using Yarn
yarn add @triyanox/async-sequence

# Using PNPM
pnpm add @triyanox/async-sequence

Usage

import createPromiseSequencer from '@triyanox/async-sequence';

// Example usage
const promiseSequencer = createPromiseSequencer(
  // add your promises here
  [() => Promise.resolve('foo'), () => Promise.reject('bar')],
  {
    // the maximum number of promises that can run at the same time
    concurrency: 2,
    // the maximum number of times a task can be retried
    retryAttempts: 3,
    // the delay between retries
    retryDelay: 1000,
    // a logger that logs the status of the PromiseSequencer
    logger: {
      log: (level, message) => {
        console.log(`[${level.toUpperCase()}] ${message}`);
      },
    },
    // callbacks for when a task is completed, failed or retried
    onTaskCompleted: (result) => {
      console.log(`Task completed with result: ${result()}`);
    },
    onTaskFailed: (error) => {
      console.log(`Task failed with error: ${error()}`);
    },
    onTaskRetried: (task) => {
      console.log(`Task retried: ${task}`);
    },
    // whether to disable logs
    disableLogs: false,
  },
);

// use a custom generator
const promiseSequencer = createPromiseSequencer(
  (function* custom() {
    yield () => Promise.resolve('foo');
    yield () => Promise.resolve('bar');
    yield () => Promise.reject(new Error('baz'));
  })(),
);

// start the PromiseSequencer
promiseSequencer.start();

// get the sequencer results
const results = await promiseSequencer.getResults();

// stop the PromiseSequencer
promiseSequencer.stop();

API

Enum: LogLevel

Represents different log levels for logging messages.

  • ERROR: Error log level.
  • INFO: Information log level.
  • DEBUG: Debug log level.

Interface: Logger

A logger interface to implement custom logging behavior.

  • log(level: LogLevel, message: string): void: Logs a message at the specified log level.

Interface: PromiseSequencerOptions<T>

Options for configuring the PromiseSequencer.

  • promiseGenerator: A generator that yields promises.
  • concurrency (optional): The number of promises to run concurrently. Default: 1.
  • retryAttempts (optional): The number of times to retry a failed task. Default: 0.
  • retryDelay (optional): The delay in milliseconds between retries. Default: 0.
  • logger (optional): A logger instance for custom logging. Default: console.
  • disableLogs (optional): Whether to disable logging. Default: false.
  • onTaskCompleted (optional): A callback for task completion.
  • onTaskFailed (optional): A callback for task failure.
  • onTaskRetried (optional): A callback for task retries.
  • throwOnErrors (optional): Whether to throw an error when a task fails. Default: false.

Class: PromiseSequencer<T>

A class for executing promises sequentially with concurrency and retry capabilities.

  • constructor(options: PromiseSequencerOptions<T>): Creates a new PromiseSequencer instance.
  • start(): Promise<boolean>: Starts executing promises.
  • stop(): Stops the PromiseSequencer.
  • getQueue(): (() => Promise<T>)[]: Returns the current queue of tasks.
  • getRunningTasks(): (() => Promise<T>)[]: Returns the current running tasks.
  • getCompletedTasks(): (() => Promise<T>)[]: Returns the completed tasks.
  • getFailedTasks(): (() => Promise<T>)[]: Returns the failed tasks.
  • getRetryTasks(): (() => Promise<T>)[]: Returns the tasks that are currently being retried.
  • getResults(): Promise<T[]>: Returns the results of the PromiseSequencer if the throwOnErrors option is set to false it will return null for the failed tasks if not it will throw an error.
  • setCurrentConcurrency(concurrency: number): void: Sets the current concurrency.

Function: createPromiseSequencer<T>

Creates a new PromiseSequencer instance.

  • promiseGenerator: A generator that yields promises.
  • options (optional): Options for configuring the PromiseSequencer.

License

This project is licensed under the MIT License - see the LICENSE file for details.