Home > Software engineering >  Does Promise.race rejects the other promises after first fulfillment?
Does Promise.race rejects the other promises after first fulfillment?

Time:05-20

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?

CodePudding user response:

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
});

  • Related