Socket
Book a DemoInstallSign in
Socket

p-race

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-race

A better `Promise.race()`

latest
Source
npmnpm
Version
3.1.0
Version published
Weekly downloads
2.8K
33.75%
Maintainers
1
Weekly downloads
 
Created
Source

p-race

A better Promise.race()

Improvements:

  • Fixes the silly behavior 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 if you like to get the first promise to fulfill.
  • Supports aborting promises using AbortSignal.

Install

npm install p-race

Usage

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

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.

import pRace from 'p-race';

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

Keywords

promise

FAQs

Package last updated on 13 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts