diff --git a/.github/funding.yml b/.github/funding.yml deleted file mode 100644 index 1a630e9..0000000 --- a/.github/funding.yml +++ /dev/null @@ -1,3 +0,0 @@ -github: sindresorhus -open_collective: sindresorhus -custom: https://sindresorhus.com/donate diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c1870cf..3b8aa86 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/index.d.ts b/index.d.ts index 3e5c62f..7905820 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 | PromiseLike; - type Options = Omit, 'count'>; - type CancelablePromise = PCancelable; - 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' - })(); - ``` - */ - ( - input: Iterable>, - options?: pAny.Options - ): pAny.CancelablePromise; - - AggregateError: typeof PSomeAggregateError; -}; - -export = pAny; +import PCancelable from 'p-cancelable'; +import {Options as PSomeOptions} from 'p-some'; + +export type Value = ValueType | PromiseLike; +export type Options = Omit, 'count'>; // eslint-disable-line @typescript-eslint/ban-types +export type CancelablePromise = PCancelable; + +/** +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( + input: Iterable>, + options?: Options +): CancelablePromise; + +export {AggregateError} from 'p-some'; diff --git a/index.js b/index.js index 3c7fe7a..6324fbe 100644 --- a/index.js +++ b/index.js @@ -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 => { @@ -13,6 +12,6 @@ module.exports = (iterable, options) => { const [value] = await anyCancelable; return value; })(); -}; +} -module.exports.AggregateError = pSome.AggregateError; +export {AggregateError} from 'p-some'; diff --git a/index.test-d.ts b/index.test-d.ts index 387ef52..2a30952 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,6 +1,5 @@ import {expectType} from 'tsd'; -import pAny = require('.'); -import {AggregateError, CancelablePromise} from '.'; +import pAny, {AggregateError, CancelablePromise} from './index.js'; expectType>(pAny([Promise.resolve(1)])); expectType>( @@ -16,5 +15,7 @@ expectType>( }) ); -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); diff --git a/package.json b/package.json index 463f359..4577fa5 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,10 @@ "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=10" + "node": ">=12.20" }, "scripts": { "test": "xo && ava && tsd" @@ -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" } } diff --git a/readme.md b/readme.md index 6018d63..a33b521 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -42,7 +40,7 @@ Returns a [cancelable `Promise`](https://github.com/sindresorhus/p-cancelable) t #### input -Type: `Iterable` +Type: `Iterable` #### options @@ -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. diff --git a/test.js b/test.js index 9830ec4..c2a35cb 100644 --- a/test.js +++ b/test.js @@ -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 = [ @@ -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}); });