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

p-reflect

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

p-reflect

Make a promise always fulfill with its actual fulfillment value or rejection reason

  • 3.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
449K
decreased by-8.32%
Maintainers
1
Weekly downloads
 
Created

What is p-reflect?

The p-reflect npm package is a utility for reflecting a promise, meaning it allows you to handle both resolved and rejected promises in a uniform way. It wraps a promise and returns an object with the status and value or reason of the promise, making it easier to handle errors and results in a consistent manner.

What are p-reflect's main functionalities?

Reflecting a Promise

This feature allows you to reflect a resolved promise. The returned object contains the status of the promise and its value.

const pReflect = require('p-reflect');

(async () => {
  const result = await pReflect(Promise.resolve('Success'));
  console.log(result); // { isFulfilled: true, isRejected: false, value: 'Success' }
})();

Handling Rejected Promises

This feature allows you to reflect a rejected promise. The returned object contains the status of the promise and the reason for rejection.

const pReflect = require('p-reflect');

(async () => {
  const result = await pReflect(Promise.reject(new Error('Failure')));
  console.log(result); // { isFulfilled: false, isRejected: true, reason: Error: Failure }
})();

Combining Multiple Promises

This feature allows you to handle multiple promises and get their statuses and results in a consistent manner.

const pReflect = require('p-reflect');

(async () => {
  const promises = [
    pReflect(Promise.resolve('Success 1')),
    pReflect(Promise.reject(new Error('Failure 1'))),
    pReflect(Promise.resolve('Success 2')),
  ];
  const results = await Promise.all(promises);
  console.log(results);
  // [
  //   { isFulfilled: true, isRejected: false, value: 'Success 1' },
  //   { isFulfilled: false, isRejected: true, reason: Error: Failure 1 },
  //   { isFulfilled: true, isRejected: false, value: 'Success 2' }
  // ]
})();

Other packages similar to p-reflect

Keywords

FAQs

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

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