What is @sapphire/async-queue?
@sapphire/async-queue is a utility for managing asynchronous tasks in a sequential manner. It ensures that tasks are executed one after another, preventing race conditions and ensuring order.
What are @sapphire/async-queue's main functionalities?
Basic Queue Usage
This code demonstrates the basic usage of @sapphire/async-queue. It creates a queue and ensures that task1 and task2 are executed sequentially.
const { AsyncQueue } = require('@sapphire/async-queue');
const queue = new AsyncQueue();
async function task1() {
await queue.wait();
console.log('Task 1 started');
setTimeout(() => {
console.log('Task 1 completed');
queue.shift();
}, 1000);
}
async function task2() {
await queue.wait();
console.log('Task 2 started');
setTimeout(() => {
console.log('Task 2 completed');
queue.shift();
}, 500);
}
task1();
task2();
Handling Errors in Queue
This code demonstrates how to handle errors within the queue. Even if a task throws an error, the queue continues to process the next task.
const { AsyncQueue } = require('@sapphire/async-queue');
const queue = new AsyncQueue();
async function taskWithError() {
await queue.wait();
console.log('Task with error started');
setTimeout(() => {
console.log('Task with error encountered an error');
queue.shift();
throw new Error('Task error');
}, 1000);
}
async function taskAfterError() {
await queue.wait();
console.log('Task after error started');
setTimeout(() => {
console.log('Task after error completed');
queue.shift();
}, 500);
}
taskWithError().catch(console.error);
taskAfterError();
Other packages similar to @sapphire/async-queue
async
The 'async' package provides a wide range of utilities for working with asynchronous JavaScript. It includes functions for parallel and sequential execution, among others. Compared to @sapphire/async-queue, 'async' offers more comprehensive functionality but can be more complex to use for simple sequential task management.
p-queue
The 'p-queue' package is a promise queue with concurrency control. It allows you to manage the execution of promises with a specified concurrency level. While 'p-queue' offers more advanced features like concurrency control, @sapphire/async-queue focuses on simple sequential execution.
queue
The 'queue' package is a simple and efficient queue implementation for Node.js. It supports basic queue operations and is easy to use. Compared to @sapphire/async-queue, it lacks built-in support for asynchronous task management but can be extended for such use cases.
Description
Ever needed a queue for a set of promises? This is the package for you.
Features
- Written in TypeScript
- Bundled with Rollup so it can be used in NodeJS and browsers
- Offers CommonJS, ESM and UMD bundles
- Fully tested
Installation
You can use the following command to install this package, or replace npm install
with your package manager of choice.
npm install @sapphire/async-queue
API Documentation
For the full API documentation please refer to the TypeDoc generated documentation.
Buy us some doughnuts
Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!
We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.
Contributors โจ
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!