Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 31, 2021
1 parent 3c8b743 commit df9b57b
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 86 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
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
80 changes: 34 additions & 46 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
import PCancelable = require('p-cancelable');
import {
Options as PSomeOptions,
AggregateError as PSomeAggregateError
} from 'p-some';

declare namespace pAny {
type Value<ValueType> = ValueType | PromiseLike<ValueType>;
type Options<ValueType> = Omit<PSomeOptions<ValueType>, 'count'>;
type CancelablePromise<ValueType> = PCancelable<ValueType>;
type AggregateError = PSomeAggregateError;
}

declare const pAny: {
/**
Wait for any promise to be fulfilled.
@param input - An `Iterable` collection of promises/values to wait for.
@returns A [cancelable `Promise`](https://github.com/sindresorhus/p-cancelable) that is fulfilled when any promise from `input` is fulfilled. If all the input promises reject, it will reject with an [`AggregateError`](https://github.com/sindresorhus/aggregate-error) error.
@example
```
import got = require('got');
import pAny = require('p-any');
(async () => {
const first = await pAny([
got.head('https://github.com').then(() => 'github'),
got.head('https://google.com').then(() => 'google'),
got.head('https://twitter.com').then(() => 'twitter'),
]);
console.log(first);
//=> 'google'
})();
```
*/
<ValueType>(
input: Iterable<pAny.Value<ValueType>>,
options?: pAny.Options<ValueType>
): pAny.CancelablePromise<ValueType>;

AggregateError: typeof PSomeAggregateError;
};

export = pAny;
import PCancelable from 'p-cancelable';
import {Options as PSomeOptions} from 'p-some';

export type Value<ValueType> = ValueType | PromiseLike<ValueType>;
export type Options<ValueType> = Omit<PSomeOptions<ValueType>, 'count'>; // eslint-disable-line @typescript-eslint/ban-types
export type CancelablePromise<ValueType> = PCancelable<ValueType>;

/**
Wait for any promise to be fulfilled.
@param input - An `Iterable` collection of promises/values to wait for.
@returns A [cancelable `Promise`](https://github.com/sindresorhus/p-cancelable) that is fulfilled when any promise from `input` is fulfilled. If all the input promises reject, it will reject with an [`AggregateError`](https://github.com/sindresorhus/aggregate-error) error.
@example
```
import pAny from 'p-any';
import got from 'got';
const first = await pAny([
got.head('https://github.com').then(() => 'github'),
got.head('https://google.com').then(() => 'google'),
got.head('https://twitter.com').then(() => 'twitter'),
]);
console.log(first);
//=> 'google'
```
*/
export default function pAny<ValueType>(
input: Iterable<Value<ValueType>>,
options?: Options<ValueType>
): CancelablePromise<ValueType>;

export {AggregateError} from 'p-some';
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
const pSome = require('p-some');
const PCancelable = require('p-cancelable');
import pSome from 'p-some';
import PCancelable from 'p-cancelable';

module.exports = (iterable, options) => {
export default function pAny(iterable, options) {
const anyCancelable = pSome(iterable, {...options, count: 1});

return PCancelable.fn(async onCancel => {
Expand All @@ -13,6 +12,6 @@ module.exports = (iterable, options) => {
const [value] = await anyCancelable;
return value;
})();
};
}

module.exports.AggregateError = pSome.AggregateError;
export {AggregateError} from 'p-some';
7 changes: 4 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {expectType} from 'tsd';
import pAny = require('.');
import {AggregateError, CancelablePromise} from '.';
import pAny, {AggregateError, CancelablePromise} from './index.js';

expectType<CancelablePromise<number>>(pAny([Promise.resolve(1)]));
expectType<CancelablePromise<number | string>>(
Expand All @@ -16,5 +15,7 @@ expectType<CancelablePromise<number>>(
})
);

const aggregateError = new AggregateError([new Error()]);
// TODO: TypeScript bug.
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const aggregateError = new AggregateError([new Error('error')]);
expectType<AggregateError>(aggregateError);
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12.20"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -39,13 +41,13 @@
"bluebird"
],
"dependencies": {
"p-cancelable": "^2.0.0",
"p-some": "^5.0.0"
"p-cancelable": "^3.0.0",
"p-some": "^6.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"delay": "^4.1.0",
"tsd": "^0.11.0",
"xo": "^0.26.1"
"ava": "^3.15.0",
"delay": "^5.0.0",
"tsd": "^0.16.0",
"xo": "^0.40.1"
}
}
28 changes: 13 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ $ npm install p-any
Checks 3 websites and logs the fastest.

```js
const got = require('got');
const pAny = require('p-any');

(async () => {
const first = await pAny([
got.head('https://github.com').then(() => 'github'),
got.head('https://google.com').then(() => 'google'),
got.head('https://twitter.com').then(() => 'twitter'),
]);

console.log(first);
//=> 'google'
})();
import pAny from 'p-any';
import got from 'got';

const first = await pAny([
got.head('https://github.com').then(() => 'github'),
got.head('https://google.com').then(() => 'google'),
got.head('https://twitter.com').then(() => 'twitter'),
]);

console.log(first);
//=> 'google'
```

## API
Expand All @@ -42,7 +40,7 @@ Returns a [cancelable `Promise`](https://github.com/sindresorhus/p-cancelable) t

#### input

Type: `Iterable<Promise|any>`
Type: `Iterable<Promise | unknown>`

#### options

Expand All @@ -54,7 +52,7 @@ Type: `Function`

Receives the value resolved by the promise. Used to filter out values that doesn't satisfy a condition.

### pAny.AggregateError
### AggregateError

Exposed for instance checking.

Expand Down
9 changes: 5 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable unicorn/error-message */
import test from 'ava';
import delay from 'delay';
import PCancelable from 'p-cancelable';
import pAny from '.';
import PCancelable, {CancelError} from 'p-cancelable';
import pAny from './index.js';

test('returns the first fulfilled value', async t => {
const fixture = [
Expand Down Expand Up @@ -55,10 +56,10 @@ test('cancels all promises when returned promise is canceled', async t => {
const promise = pAny(fixture);
promise.cancel();

await t.throwsAsync(promise, PCancelable.CancelError);
await t.throwsAsync(promise, {instanceOf: CancelError});
t.deepEqual(canceled, [true, true]);
});

test('rejects on empty iterable', async t => {
await t.throwsAsync(pAny([]), RangeError);
await t.throwsAsync(pAny([]), {instanceOf: RangeError});
});

0 comments on commit df9b57b

Please sign in to comment.