Nikos Iliakis
Nikos Iliakis

Reputation: 167

Does Promise.race rejects the other promises after first fulfillment?

Something that I cannot find the exact answer. In Promise.race after the first fulfillment takes place, do the rest promises keep running or they are rejected?

Upvotes: 2

Views: 326

Answers (1)

Sergiy Ostrovsky
Sergiy Ostrovsky

Reputation: 2532

The answer is yes, they keep running. You can see the answer yourself

const promise1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    console.log('completing promise1 after 1 sec')
    resolve('one');
  }, 1000);
});

const promise2 = new Promise((resolve, reject) => {
  setTimeout(() => {
    console.log('completing promise2 after .5 sec')
    resolve('two');
  }, 500);
});

Promise.race([promise1, promise2]).then((value) => {
  console.log(value);
  // Both resolve, but promise2 is faster
});

Upvotes: 3

Related Questions