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

tasque

v0.1.2

Published

A simple TypeScript task queue

Downloads

105

Readme

tasque

A simple TypeScript task queue.

This library is built with low overhead in mind: bundle size

Features

Installation

You can install this package using your favorite package manager from npm or jsr.

You can pick one of the following commands:

# npm
npm install tasque
bun install tasque
pnpm install tasque
yarn install tasque
deno add npm:tasque
# jsr
npx jsr add @sv2dev/tasque
bunx jsr add @sv2dev/tasque
pnpm dlx jsr add @sv2dev/tasque
yarn dlx jsr add @sv2dev/tasque
deno add jsr:@sv2dev/tasque

Usage

import { createQueue } from "tasque";

const queue = createQueue();

Sequential execution

In this example, the tasks are executed one after the other.

queue.add(async () => {});
queue.add(async () => {});

Synchronous tasks

You can also add synchronous tasks to the queue.

queue.add(async () => {});
queue.add(() => {});

Parallel execution

In this example, always two tasks are executed in parallel. If one task is finished, another one is started.

const queue = createQueue({ parallelize: 2 });

queue.add(async () => {});
queue.add(async () => {});
queue.add(async () => {});
queue.add(async () => {});

You can also use the parallelize property to change the number of parallel tasks at runtime.

Scaling up the number of parallel tasks will immediately start queued tasks to match the new parallel count.

// schedule some tasks
queue.parallelize = 3;
// more of these tasks will be executed in parallel

Scaling down the number of parallel tasks will not affect already running tasks, but queued tasks will wait until the number of running tasks is reduced to the new parallel count.

const queue = createQueue({ parallelize: 3 });
// schedule some tasks
queue.parallelize = 1;
// less tasks will be executed in parallel when the running tasks are finished

Queue capacity

The queue will reject new tasks if it is full. By default, the queue can hold an arbitrary number of tasks. But the capacity can be limited by setting the max option.

const queue = createQueue({ max: 2 });

const res1 = queue.add(async () => {});
const res2 = queue.add(async () => {});
const res3 = queue.add(async () => {});

// res1 and res2 are Promises that resolve when the task is finished.
// res3 is null, because the queue is full.

You can also change the capacity at runtime. This will not affect already queued tasks.

queue.max = 1;

React to queue position changes

In this example, a position listener is passed to the add method. It will be called every time the queue position changes.

queue.add(
  async () => {},
  (pos) => {
    if (pos === 0) {
      console.log(`Task is no longer queued and running`);
    } else {
      console.log(`This task is at queue position ${pos}`);
    }
  }
);

Alternatively, you can also use the listener option to pass a listener function to the add method.

queue.add(async () => {}, {
  listener: (pos) => {
    console.log(`This task is at queue position ${pos}`);
  },
});

Stream queue position and values

In this example, the task will stream the queue position and the task values.

const iterable = queue.iterate(async () => {});

for await (const [pos, value] of iterable!) {
  if (pos === null) {
    console.log(`Task emitted a value: ${value}`);
  } else if (pos === 0) {
    console.log(`Task is no longer queued and running`);
  } else {
    console.log(`Task is at queue position ${pos}`);
  }
}

Streaming tasks

Tasks that not only return a result but yield values can be streamed as well.

const iterable = queue.iterate(async function* () {
  yield "Hello,";
  yield "world!";
});

for await (const [pos, value] of iterable!) {
  if (pos === null) {
    console.log(`The task yielded this value: ${value}`);
  }
}

This can also be used with the add method. In this case, only the last yielded value is returned.

const result = await queue.add(async function* () {
  yield "Hello,";
  yield "world!";
});

console.log(result); // "world!"

Discard tasks

There are several ways to discard tasks.

Breaking iteration

If you have an iterable, you can break the iteration to discard the task.

const iterable = queue.iterate(async () => {});

for await (const [pos, value] of iterable!) {
  break; // Discards the task
}

This only works if the task is not already running.

Aborting tasks

If you pass an AbortSignal to the add or iterate method, the task will be discarded if the signal is aborted.

const ctrl = new AbortController();
const promise = queue.add(async () => {}, { signal: ctrl.signal });

You can also pass the AbortSignal directly as the options object.

const ctrl = new AbortController();
const promise = queue.add(async () => {}, ctrl.signal);

For example, if you want to discard a task after a certain time, you can do something like this:

const promise = queue.add(async () => {}, AbortSignal.timeout(10_000));