Race multiple async operations with automatic AbortSignal cleanup for losers
npm install abort-raceimport abortRace from 'abort-race';
const result = await abortRace([
async signal => {
const response = await fetch('https://api1.example.com', {signal});
return response.json();
},
async signal => {
const response = await fetch('https://api2.example.com', {signal});
return response.json();
},
]);
console.log(result);
// The response from whichever API responded first.
// The other request was automatically aborted.Race multiple async operations. The first task to resolve wins, and all other tasks are automatically aborted via their AbortSignal. If a task throws, all others are aborted and the error is re-thrown.
Returns a Promise that resolves with the winning task's result.
Type: Array<(signal: AbortSignal) => Promise<T>>
An array of functions that each receive an AbortSignal and return a Promise.
Type: object
Type: AbortSignal
An external AbortSignal for cancelling all tasks. When this signal aborts, all tasks are aborted.
- p-race - Race promises
- signal-compose - Compose multiple AbortSignals
MIT