🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

promise-timeout

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-timeout

Simple timeouts for promises

1.3.0
latest
Source
npm
Version published
Weekly downloads
276K
-3.23%
Maintainers
1
Weekly downloads
 
Created

What is promise-timeout?

The `promise-timeout` npm package provides utilities to add timeout functionality to promises. It allows you to set a time limit on a promise, after which it will be rejected if not resolved. This is useful for handling cases where you want to ensure that certain asynchronous operations do not take longer than a specified duration.

What are promise-timeout's main functionalities?

Timeout a Promise

This feature allows you to set a timeout on a promise. If the promise does not resolve within the specified time (1000ms in this case), it will be rejected with a timeout error.

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

const myPromise = new Promise((resolve) => {
  setTimeout(() => resolve('Success!'), 2000);
});

const timeoutPromise = timeout(myPromise, 1000);

timeoutPromise
  .then((result) => console.log(result))
  .catch((err) => console.error('Promise timed out:', err));

Timeout with Custom Error

This feature allows you to set a timeout on a promise with a custom error message. If the promise does not resolve within the specified time (1000ms in this case), it will be rejected with the custom timeout error.

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

const myPromise = new Promise((resolve) => {
  setTimeout(() => resolve('Success!'), 2000);
});

const timeoutPromise = timeout(myPromise, 1000, new TimeoutError('Custom timeout error'));

timeoutPromise
  .then((result) => console.log(result))
  .catch((err) => console.error('Promise timed out:', err.message));

Other packages similar to promise-timeout

Keywords

promise

FAQs

Package last updated on 18 Feb 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