p-reflect
Make a promise always fulfill with its actual fulfillment value or rejection reason
Last updated 6 years ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install p-reflect 
SYNC missed versions from official npm registry.

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);
/*
[
	{
		status: 'fulfilled',
		value: '🦄'
		isFulfilled: true,
		isRejected: false
	},
	{
		status: 'rejected',
		reason: [Error: 👹]
		isFulfilled: false,
		isRejected: true
	},
	{
		status: 'fulfilled',
		value: '🐴'
		isFulfilled: true,
		isRejected: false
	}
]
*/

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:

  • status ('fulfilled' or 'rejected', depending on how the promise resolved)
  • value or reason (Depending on whether the promise fulfilled or rejected)
  • isFulfilled
  • isRejected

promise

Type: Promise

A promise to reflect upon.

isFulfilled(object)

This is a type guard for TypeScript users.

Returns true if the object has the property value, false otherwise.

This is useful since await pReflect(promise) always returns a PromiseResult. This function can be used to determine whether PromiseResult is PromiseFulfilledResult or PromiseRejectedResult.

This is a workaround for microsoft/TypeScript#32399

isRejected(object)

This is a type guard for TypeScript users.

Returns true if the object has the property reason, false otherwise.

This is useful since await pReflect(promise) always returns a PromiseResult. This function can be used to determine whether PromiseResult is PromiseRejectedResult or PromiseFulfilledResult.

Related

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

Current Tags

  • 3.1.0                                ...           latest (2 years ago)

5 Versions

  • 3.1.0                                ...           2 years ago
  • 3.0.0                                ...           4 years ago
  • 2.1.0                                ...           6 years ago
  • 2.0.0                                ...           6 years ago
  • 1.0.0                                ...           8 years ago
Maintainers (1)
Downloads
Total 4
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (3)

© 2010 - cnpmjs.org x YWFE | Home | YWFE