race

async

Creates a promise that resolves or rejects as soon as one of the promises resolves or rejects. Enhanced version of Promise.race with better error handling.

Signature

race<T>(promises: Promise<T>[]): Promise<T>

Parameters

NameTypeDescription
promisesPromise<T>[]Array of promises to race

Returns

Promise<T> - Returns the first settled promise result

Examples

Race API endpoints

import { race } from 'dashlite'

const result = await race([
  fetch('https://api1.example.com'),
  fetch('https://api2.example.com')
])

Fastest response

import { race } from 'dashlite'

const fastest = await race([
  delay(100, 'fast'),
  delay(500, 'slow')
])
console.log(fastest)

Output:

'fast'

Available since version 1.2.0