New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

promise-toolbox

Package Overview
Dependencies
Maintainers
3
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-toolbox

Essential utils for promises

0.10.1
Source
npm
Version published
Weekly downloads
141K
-2.27%
Maintainers
3
Weekly downloads
 
Created

What is promise-toolbox?

The promise-toolbox npm package provides a set of utilities to work with JavaScript promises more effectively. It offers various tools to handle promise-related tasks such as timeouts, retries, and concurrency control, making it easier to manage asynchronous operations.

What are promise-toolbox's main functionalities?

Timeouts

The timeout feature allows you to set a maximum time limit for a promise to resolve. If the promise does not resolve within the specified time, it will be rejected with a timeout error. This is useful for preventing long-running operations from hanging indefinitely.

const { timeout } = require('promise-toolbox');

async function fetchData() {
  // Simulate a long-running operation
  return new Promise(resolve => setTimeout(() => resolve('Data'), 5000));
}

async function fetchDataWithTimeout() {
  try {
    const data = await timeout(fetchData(), 2000);
    console.log(data);
  } catch (error) {
    console.error('Operation timed out');
  }
}

fetchDataWithTimeout();

Retries

The retry feature allows you to automatically retry a promise-returning function a specified number of times if it fails. This is useful for handling transient errors in operations like network requests.

const { retry } = require('promise-toolbox');

async function unstableOperation() {
  // Simulate an operation that may fail
  if (Math.random() < 0.7) throw new Error('Failed');
  return 'Success';
}

async function performWithRetries() {
  try {
    const result = await retry(unstableOperation, { retries: 5 });
    console.log(result);
  } catch (error) {
    console.error('Operation failed after retries');
  }
}

performWithRetries();

Concurrency Control

The concurrency control feature allows you to limit the number of concurrent promise executions. This is useful for managing resource usage when performing multiple asynchronous operations, such as API requests.

const { map } = require('promise-toolbox');

async function fetchData(id) {
  // Simulate a network request
  return new Promise(resolve => setTimeout(() => resolve(`Data for ${id}`), 1000));
}

async function fetchAllData() {
  const ids = [1, 2, 3, 4, 5];
  const results = await map(ids, fetchData, { concurrency: 2 });
  console.log(results);
}

fetchAllData();

Other packages similar to promise-toolbox

Keywords

promise

FAQs

Package last updated on 06 Sep 2018

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