Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
Does Promise.race ignore promises after the first promise resolves

code snippet

I have this code snippet, the timeoutPromise is supposed to resolve after 250ms IF the responsePromise does not resolve in that amount of time. Right now I see the timeoutPromise resolves regardless of the responsePromise.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

All Promise.race() does it report to the caller which of the promises you passed it resolved/rejected first. It does not affect whether they resolve/reject and does not stop the other promises from resolving after the first one has resolved. The operations in those other promises still keep going and they have whatever outcome they would have, regardless of using Promise.race().

Look at this example which you can run right in the snippet:

function resolveMs(ms) {
    return new Promise(resolve => {
        setTimeout(() => {
            console.log(`resolveMs(${ms}) resolving`);
            resolve(ms);
        }, ms);
    });
}

console.log('Starting...');
Promise.race([resolveMs(100), resolveMs(50), resolveMs(200)]).then(result => {
    console.log(`Final result: ${result}`);
});

That generates this output:

resolveMs(50) resolving
Final result: 50
resolveMs(100) resolving
resolveMs(200) resolving

There you can clearly see that all three promises resolve on their expected schedule. Promise.race() itself resolves when the first of the three promises you passed it resolve, but that does not affect the other two operations. They keep doing what they were doing.

You can think of it like an 800m running race. Promise.race() reports the winner of the race (that is its job), but just because there's a winner, the other participants don't stop running. They still finish the race too in whatever time they would have if they ran on their own.

If you want the other operations to somehow stop when the first resolves, you'd have to manually code things so that you cancelled the other two operations and they would have to represent operations that are cancellable as there is no automatic, built-in mechanism for stopping asynchronous operations that are already underway. In my example above, one would have to call clearTimeout() on the remaining timers (and you would have to save the timerID values somewhere so you could call clearTimeout() on them).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!