# p-reflect

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

Latest version **3.1.0** (published 2022-07-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install p-reflect
pnpm add p-reflect
yarn add p-reflect
bun add p-reflect
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.0 |
| Published | 2022-07-25 |
| First published | 2016-11-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=12 |
| Dependencies | 0 |
| Unpacked size | 6.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 56 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | promise, reflect, inspect, debug, fulfill, reject, fulfilled, rejected, error, async, await, promises, bluebird |

## Links

- npm: https://www.npmjs.com/package/p-reflect
- Repository: https://github.com/sindresorhus/p-reflect
- Homepage: https://github.com/sindresorhus/p-reflect#readme
- Issues: https://github.com/sindresorhus/p-reflect/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/p-reflect

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 3.1.0 (latest) — 2022-07-25
- 3.0.0 — 2021-04-08
- 2.1.0 — 2019-04-03
- 2.0.0 — 2019-03-18
- 1.0.0 — 2016-11-26

## README

# 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.

```js
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`](https://github.com/sindresorhus/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](https://github.com/microsoft/TypeScript/issues/32399)
- reference documentation [Using type predicates](https://www.typescriptlang.org/docs/handbook/2/narrowing.html)

### 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](https://github.com/sindresorhus/p-settle) - Settle promises concurrently and get their fulfillment value or rejection reason
- [More…](https://github.com/sindresorhus/promise-fun)

---
_Source: https://npm.io/package/p-reflect · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
