You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

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


Version published
Weekly downloads
6.4M
decreased by-0.58%
Maintainers
1
Install size
3.75 kB
Created
Weekly downloads
 

Package description

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

Readme

Source

p-defer

Create a deferred promise

Don't use this unless you know what you're doing. Prefer the Promise constructor.

Install

npm install p-defer

Usage

import pDefer from 'p-defer';

function delay(milliseconds) {
	const deferred = pDefer();
	setTimeout(deferred.resolve, milliseconds, '🦄');
	return deferred.promise;
}

console.log(await delay(100));
//=> '🦄'

The above is just an example. Use delay if you need to delay a promise.

API

pDefer()

Returns an object with a promise property and functions to resolve() and reject().

  • p-lazy - Create a lazy promise that defers execution until .then() or .catch() is called
  • More…

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc