# p-race

> A better `Promise.race()`

Latest version **3.1.0** (published 2022-12-13) · MIT license · 0 weekly downloads

## Install

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

## 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-12-13 |
| First published | 2016-10-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=12 |
| Dependencies | 1 |
| Unpacked size | 5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | promise, race, better, async, await, promises, bluebird |

## Links

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

## Dependencies (1)

- [is-empty-iterable](https://npm.io/package/is-empty-iterable.md) ^3.0.0

## 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-12-13
- 3.0.0 — 2021-04-09
- 2.1.0 — 2019-04-03
- 2.0.0 — 2019-03-18
- 1.0.0 — 2016-10-21

## README

# p-race

> A better [`Promise.race()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/race)

Improvements:
- Fixes the [silly behavior](https://github.com/domenic/promises-unwrapping/issues/75) of `Promise.race()` returning a forever pending promise when supplied an empty iterable, which could create some really hard to debug problems. `Promise.race()` returns the first promise to fulfill or reject. Check out [`p-any`](https://github.com/sindresorhus/p-any) if you like to get the first promise to fulfill.
- Supports aborting promises using [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal).

## Install

```sh
npm install p-race
```

## Usage

```js
import pRace from 'p-race';

Promise.race([]);
// Returns a forever pending promise…

pRace([]);
//=> [RangeError: Expected the input to contain at least one item]
```

## API

### pRace(iterable | executor)

#### iterable

Type: `Iterable<Promise | unknown>`

#### executor

Type: `signal => Iterable<Promise | unknown>`

##### signal

Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)

You can pass the `signal` to each iterable's element to abort remaining promises when resolve the first promise.

*Requires Node.js 16 or later.*

```js
import pRace from 'p-race';

pRace(signal => [
	fetch('/api', {signal}),
	setTimeout(10, {signal}),
]);
// Remaining promises other than first one will be aborted.
```

## Related

- [p-any](https://github.com/sindresorhus/p-any) - Wait for any promise to be fulfilled
- [More…](https://github.com/sindresorhus/promise-fun)

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