Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 8, 2021
1 parent 5f2293f commit a30a0be
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 141 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Expand Up @@ -12,11 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
129 changes: 57 additions & 72 deletions index.d.ts
@@ -1,84 +1,69 @@
declare namespace pReflect {
interface PromiseFulfilledResult<ValueType> {
isFulfilled: true;
isRejected: false;
value: ValueType;
}

interface PromiseRejectedResult {
isFulfilled: false;
isRejected: true;
reason: unknown;
}

type PromiseResult<ValueType> =
| PromiseFulfilledResult<ValueType>
| PromiseRejectedResult;
export interface PromiseFulfilledResult<ValueType> {
isFulfilled: true;
isRejected: false;
value: ValueType;
}

declare const pReflect: {
/**
Make a promise always fulfill with its actual fulfillment value or rejection reason.
export interface PromiseRejectedResult {
isFulfilled: false;
isRejected: true;
reason: unknown;
}

@param promise - A promise to reflect upon.
@returns Promise reflection.
export type PromiseResult<ValueType> =
| PromiseFulfilledResult<ValueType>
| PromiseRejectedResult;

@example
```
import pReflect = require('p-reflect');
/**
Make a promise always fulfill with its actual fulfillment value or rejection reason.
// 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.
@param promise - A promise to reflect upon.
@returns Promise reflection.
(async () => {
const promises = [
getPromise(),
getPromiseThatRejects(),
getPromise()
];
@example
```
import pReflect from 'p-reflect';
const results = await Promise.all(promises.map(pReflect));
// 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.
console.log(results);
/*
[
{
isFulfilled: true,
isRejected: false,
value: '🦄'
},
{
isFulfilled: false,
isRejected: true,
reason: [Error: 👹]
},
{
isFulfilled: true,
isRejected: false,
value: '🐴'
}
]
*\/
const promises = [
getPromise(),
getPromiseThatRejects(),
getPromise()
];
const resolvedString = results
.filter(result => result.isFulfilled)
.map(result => result.value)
.join('');
const results = await Promise.all(promises.map(pReflect));
console.log(resolvedString);
//=> '🦄🐴'
})();
```
*/
<ValueType>(promise: PromiseLike<ValueType>): Promise<
pReflect.PromiseResult<ValueType>
>;
console.log(results);
/*
[
{
isFulfilled: true,
isRejected: false,
value: '🦄'
},
{
isFulfilled: false,
isRejected: true,
reason: [Error: 👹]
},
{
isFulfilled: true,
isRejected: false,
value: '🐴'
}
]
*\/
// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function pReflect<ValueType>(
// promise: PromiseLike<ValueType>
// ): Promise<pReflect.PromiseResult<ValueType>>;
// export = pReflect;
default: typeof pReflect;
};
const resolvedString = results
.filter(result => result.isFulfilled)
.map(result => result.value)
.join('');
export = pReflect;
console.log(resolvedString);
//=> '🦄🐴'
```
*/
export default function pReflect<ValueType>(promise: PromiseLike<ValueType>): Promise<
PromiseResult<ValueType>
>;
11 changes: 3 additions & 8 deletions index.js
@@ -1,8 +1,7 @@
'use strict';

const pReflect = async promise => {
export default async function pReflect(promise) {
try {
const value = await promise;

return {
isFulfilled: true,
isRejected: false,
Expand All @@ -15,8 +14,4 @@ const pReflect = async promise => {
reason: error
};
}
};

module.exports = pReflect;
// TODO: Remove this for the next major release
module.exports.default = pReflect;
}
2 changes: 1 addition & 1 deletion index.test-d.ts
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import pReflect = require('.');
import pReflect from './index.js';

const result = await pReflect(Promise.resolve('foo'));

Expand Down
2 changes: 1 addition & 1 deletion license
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
13 changes: 8 additions & 5 deletions package.json
Expand Up @@ -4,13 +4,16 @@
"description": "Make a promise always fulfill with its actual fulfillment value or rejection reason",
"license": "MIT",
"repository": "sindresorhus/p-reflect",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -35,8 +38,8 @@
"bluebird"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
89 changes: 39 additions & 50 deletions readme.md
Expand Up @@ -4,63 +4,58 @@
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
const pReflect = require('p-reflect');

(async () => {
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);
//=> '🦄🐴'
})();
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`](https://github.com/sindresorhus/p-settle) if you need this.

The above is just an example. Use [`p-settle`](https://github.com/sindresorhus/p-settle) if you need exactly that.

## API

Expand All @@ -80,13 +75,7 @@ Type: `Promise`

A promise to reflect upon.


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


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
2 changes: 1 addition & 1 deletion test.js
@@ -1,5 +1,5 @@
import test from 'ava';
import pReflect from '.';
import pReflect from './index.js';

const fixture = Symbol('fixture');

Expand Down

0 comments on commit a30a0be

Please sign in to comment.