Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sapphire/async-queue

Package Overview
Dependencies
Maintainers
0
Versions
840
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sapphire/async-queue

Sequential asynchronous lock-based queue for promises

  • 1.5.6-next.3d59d8ea
  • next
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
238K
decreased by-3.4%
Maintainers
0
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 08 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc