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

p-defer

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-defer

Create a deferred promise

  • 4.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is p-defer?

The p-defer package is a utility for creating deferred promises in JavaScript. It allows you to create a promise and expose its resolve and reject functions, which can be called later to settle the promise. This is useful for scenarios where you need to control the timing of when a promise is resolved or rejected.

What are p-defer's main functionalities?

Creating a Deferred Promise

This feature allows you to create a deferred promise and resolve it at a later time. The `pDefer` function returns an object with a `promise` property and `resolve` and `reject` methods. You can use the `promise` property to await the promise and call `resolve` or `reject` to settle it.

const pDefer = require('p-defer');

const deferred = pDefer();

// Use the promise
async function example() {
  await deferred.promise;
  console.log('Promise resolved!');
}

example();

// Resolve the promise later
setTimeout(() => {
  deferred.resolve();
}, 1000);

Handling Errors with Deferred Promises

This feature demonstrates how to handle errors with deferred promises. You can call the `reject` method to reject the promise with an error, and handle the error using a try-catch block when awaiting the promise.

const pDefer = require('p-defer');

const deferred = pDefer();

// Use the promise
async function example() {
  try {
    await deferred.promise;
  } catch (error) {
    console.error('Promise rejected:', error);
  }
}

example();

// Reject the promise later
setTimeout(() => {
  deferred.reject(new Error('Something went wrong'));
}, 1000);

Other packages similar to p-defer

Keywords

FAQs

Package last updated on 01 Apr 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