FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

Promise Race

15mins

Implement a function promiseRace(promises) that behaves like Promise.race().

The function should:

  • Accept an array of promises (or values).
  • Return a promise that resolves or rejects as soon as one of the input promises resolves or rejects.
  • If a non-promise value is in the array, treat it as a resolved promise.

Examples

const p1 = new Promise((res) => setTimeout(() => res(1), 100)); const p2 = new Promise((res) => setTimeout(() => res(2), 50)); promiseRace([p1, p2]).then(console.log); // 2 const p3 = Promise.reject('error'); promiseRace([p3, p1]).catch(console.log); // 'error'

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests