Blog/NotesConcept

Promise.race Polyfill in Javascript - Detailed Explanation

Detailed step-by-step explanation of Promise.race polyfill in javascript to understand its internal working and handling of race conditions among promises.

Expert

Anuj Sharma

Last Updated Jun 15, 2026


Promise.race Polyfill in Javascript - Detailed Explanation

Table of Content

  1. How does Promise.race work in javascript?
  2. Promise.race Polyfill in Javascript - Detailed Explanation

# How does Promise.race work in JavaScript?

Promise.race takes the iterable (such as array) of promises, and returns the first settled promise, here settled promise can be resolved or rejected. As name "race" suggested, whichever promise settled first will be returned by Promise.race() method.

Examples

Example 1: First settled promise: Rejected

In the below example the P3 promise rejected in the minimum time 50ms, that's why it get rejected before the other 2 promises and race will return the first settled (either resolved or rejected) promise.

// First settled promise - rejected
const p1 = new Promise((resolve) => setTimeout(() => resolve("P1 resolved"), 100));
const p2 = new Promise((resolve) => setTimeout(() => resolve("P2 resolved"), 200));
const p3 = new Promise((resolve, reject) => setTimeout(() => reject("P3 rejected"), 50));

Promise.race([p1, p2, p3])
    .then((value) => console.log(`Fulfilled: ${value}`)) 
    .catch((error) => console.log(`Error: ${error}`));


// Output
"Error: P3 rejected"

Example 2: First settled promise - Resolved

In the below example the P1 promise resolves in the minimum time 100ms, that's why it resolves before the other 2 promises and race will return the first settled (either resolved or rejected) promise.

// First settled promise - Resolved
const p1 = new Promise((resolve) => setTimeout(() => resolve("P1 resolved"), 100));
const p2 = new Promise((resolve) => setTimeout(() => resolve("P2 resolved"), 200));
const p3 = new Promise((resolve, reject) => setTimeout(() => reject("P3 rejected"), 150));

Promise.race([p1, p2, p3])
    .then((value) => console.log(`Fulfilled: ${value}`)) 
    .catch((error) => console.log(`Error: ${error}`));


// Output
"Fulfilled: P1 resolved"

Expected Functionality for Custom Promise.race Implementation

Before understanding the custom implementation of Promise.race(), its important to understand the scenarios which Promise.race() needs to fulfill

1. First settled promise should be retuned (Resolved or Rejected)

// Promise resolved fist
const p1 = new Promise((resolve) => setTimeout(() => resolve("P1 resolved"), 50));
const p2 = new Promise((resolve) => setTimeout(() => resolve("P2 resolved"), 100));
Promise.race([p1, p2]).then(console.log); // Output: "P1 resolved"

// Promise rejected first
const p3 = new Promise((resolve, reject) => setTimeout(() => resolve("P3 rejected"), 50));
const p4 = new Promise((resolve) => setTimeout(() => resolve("P4 resolved"), 100));
Promise.race([p3, p4]).then(console.log); // Output: "P3 rejected"

2. Non-promise values should resolved immediately

If iterable contains any non-promise values (example - 45, 'Apple'), then these non-promise values should resolved immediately.

const p1 = new Promise((resolve) => setTimeout(() => resolve("P1 resolved"), 50));

Promise.race([42, p1]).then(console.log); // Output: 42

3. Immediate rejecting promise, will caught first

In case, if immediate rejecting promise (Promise.reject) passed It will caught first by Promise.race()

const p1 = new Promise((resolve) => setTimeout(() => resolve("P1 resolved"), 50));
const pImmediateReject = Promise.reject("Immediate rejection");

Promise.race([p1, pImmediateReject]).catch(console.log); 
// Output: "Immediate rejection"


const p2 = new Promise((resolve, reject) => setTimeout(() => reject("P2 reject"), 50));

Promise.race([pImmediateReject, p2]).catch(console.log); 
// Output: "Immediate rejection"

4. Throw error in case of invalid input

If an input is not iterable, it should throw a TypeError.

Promise.race(45)
  .then((value) => console.log(value))
  .catch((error) => console.log(error)); 

// TypeError: number 45 is not iterable (cannot read property Symbol(Symbol.iterator))

5. In case of Empty array

In case of Empty array, the return promise from Promise.race() will never settled.

Promise.race([])
  .then((value) => console.log(value))
  .catch((error) => console.log(error)); 

// Output: Returned promise never settled to return any value or error

# Promise.race Polyfill in Javascript - Detailed Explanation

Step 1: Create a custom function which takes an iterable of promises.

Step 2: It returns a new Promise that resolves or rejects as soon as one of the promises in the iterable settles (resolves or rejects).

Step 3: In this step, Loop over the iterable of Promises, and wrapped each promise in Promise.resolve() to handle non-promise values.

First settled promise's result is used to settle the returned promise.

Promise.race Polyfill Implementation Code

function customRace(promises) {
    return new Promise((resolve, reject) => {
        if (!Array.isArray(promises)) {
            return reject(new TypeError("Argument must be an iterable"));
        }

        for (const promise of promises) {
            Promise.resolve(promise).then(resolve, reject);
        }
    });
}

Learn Next

1️⃣ Promise Polyfill in JavaScript - Step by Step Explanation

2️⃣ Promise.all Polyfill in JavaScript - Detailed Explanation [For Interviews]

3️⃣ Promise.allSettled Polyfill in JavaScript - Step by Step Explanation

4️⃣ Promise.race polyfill in JavaScript explained

5️⃣ Promise.any polyfill in JavaScript explained

6️⃣ Notes to Master Promise Methods in JavaScript: all(), allSettled(), race() and any()

Love this Blog? Share it Now!

Help others discover this resource

About the Author

Anuj Sharma

A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me  🚀


Learn Next

Featured

Promise Polyfill in JavaScript - Step by Step Explanation

Top 10 React Performance Optimization Techniques25 Top JavaScript Interview Questions for BeginnersHow to create custom useInfiniteScroll Hook in ReactImplement useThrottle Custom Hook In React

Comments

Be the first to share your thoughts!

Guest User

Please login to comment

0 characters


No comments yet.

Start the conversation!

About the Author

Anuj Sharma

A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me  🚀

Share your expertise

Publish a blog or quick notes on topics you know well — your write-up could be the answer someone needs before their next frontend interview.

Build your portfolio

Help the community

Sharpen your skills

Earn goodies

Other Related Blogs

React Hook Rules: Why hooks declarations are not allowed inside functions

Frontendgeek

Last Updated Feb 6, 2026

A quick guide to explain an important react interview question, why React Hooks declarations are not allowed inside functions or any conditional blocks with code example.

Implementing a stopwatch using React - Frontend Machine Coding Question

Pallavi Gupta

Last Updated Feb 21, 2026

Concise explanation of stopwatch implementation using React, it involves the usage of useEffect hook for creating a stopwatch and tracking milliseconds.

Implement useClickOutside() custom Hook in React [Interview]

Anuj Sharma

Last Updated Dec 23, 2025

Understand the implementation of useClickOutside() custom hook in react and how it can be used to implement Modal like functionality.

Best Frontend System Design Interview Cheat Sheet 📒

Anuj Sharma

Last Updated Jun 9, 2026

A Comprehensive Frontend System Design Cheat Sheet helps you approach the Frontend System Design Interview in the most structured way and covers the 7 most important Frontend System Design Topics.

Stay Updated

Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.

FrontendGeek
FrontendGeek

All in One Preparation Hub to Ace Frontend Interviews. Master JavaScript, React, System Design, and more with curated resources.

Consider Supporting this Free Platform

Buy Me a Coffee

Product

HomeFrontend InterviewFrontend JobsQuestionsNewInterview ExperienceBlogsToolsLeaderboardFrontendGeek Chrome extensionGet the extension on the Chrome Web Store

© 2026 FrontendGeek. All rights reserved