You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

p-cancelable

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-cancelable

Create a promise that can be canceled

4.0.1
latest
Source
npm
Version published
Weekly downloads
21M
0.59%
Maintainers
1
Weekly downloads
 
Created

What is p-cancelable?

The p-cancelable package provides a way to create cancelable promises in JavaScript. This is particularly useful for managing asynchronous operations that may need to be canceled before they complete, such as HTTP requests, long-running computations, or any other task that might not need to continue executing if certain conditions are met.

What are p-cancelable's main functionalities?

Creating a cancelable promise

This feature allows the creation of a new cancelable promise. The promise can be canceled at any time before it resolves or rejects, which triggers the onCancel function where cleanup operations like clearing timeouts or aborting requests can be performed.

const PCancelable = require('p-cancelable');

const myCancelablePromise = new PCancelable((resolve, reject, onCancel) => {
  const timer = setTimeout(() => {
    resolve('Done!');
  }, 2000);

  onCancel(() => {
    clearTimeout(timer);
    reject(new Error('Operation canceled.'));
  });
});

myCancelablePromise.cancel();

Other packages similar to p-cancelable

Keywords

promise

FAQs

Package last updated on 19 Jul 2022

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