Skip to content

axetroy/async-queue.js

Repository files navigation

async-queue.js

Badge LICENSE Node npm version

A library to limit the number of concurrent asynchronous tasks.

The queue will start immediately when the task is added to the queue.

Installation

npm install @axetroy/async-queue --save

Usage

// import via esm
import { AsyncQueue } from "@axetroy/async-queue";

// import via cjs
const { AsyncQueue } = require("@axetroy/async-queue");
import { AsyncQueue } from "@axetroy/async-queue";

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const queue = new AsyncTaskQueue({ concurrency: 2 });
// Add task to the queue
queue.enqueue(() => delay(1000).then(() => console.log("Task 1 completed")));
queue.enqueue(() => delay(500).then(() => console.log("Task 2 completed")));
queue.enqueue(() => delay(300).then(() => console.log("Task 3 completed")));
queue.enqueue(() => delay(400).then(() => console.log("Task 4 completed")));

// Wait for all tasks to complete
await queue.waitForAll();

License

The Anti 996 License

About

A library to limit the number of concurrent asynchronous tasks

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy