Socket
Socket
Sign inDemoInstall

p-reflect

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

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


Version published
Maintainers
1
Install size
6.04 kB
Created

Readme

Source

p-reflect

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

Useful when you want a promise to fulfill no matter what and would rather handle the actual state afterwards.

Install

$ npm install p-reflect

Usage

Here, Promise.all would normally fail early because one of the promises rejects, but by using p-reflect, we can ignore the rejection and handle it later on.

import pReflect from 'p-reflect';

const promises = [
	getPromise(),
	getPromiseThatRejects(),
	getPromise()
];

const results = await Promise.all(promises.map(pReflect));

console.log(results);
/*
[
	{
		isFulfilled: true,
		isRejected: false,
		value: '🦄'
	},
	{
		isFulfilled: false,
		isRejected: true,
		reason: [Error: 👹]
	},
	{
		isFulfilled: true,
		isRejected: false,
		value: '🐴'
	}
]
*/

const resolvedString = results
	.filter(result => result.isFulfilled)
	.map(result => result.value)
	.join('');

console.log(resolvedString);
//=> '🦄🐴'

The above is just an example. Use p-settle if you need exactly that.

API

pReflect(promise)

Returns a Promise<Object>.

The object has the following properties:

  • isFulfilled
  • isRejected
  • value or reason (Depending on whether the promise fulfilled or rejected)
promise

Type: Promise

A promise to reflect upon.

  • p-settle - Settle promises concurrently and get their fulfillment value or rejection reason
  • More…

Keywords

FAQs

Last updated on 08 Apr 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc