Sobes.tech
Middle

What method of a promise is used to get the result of the first completed request?

sobes.tech AI

Answer from AI

To get the result of the first fulfilled promise, the Promise.race() method is used. It takes an array of promises and returns a new promise that resolves or rejects with the result of the first promise that settles.

Example:

const promise1 = new Promise(resolve => setTimeout(() => resolve('First'), 500));
const promise2 = new Promise(resolve => setTimeout(() => resolve('Second'), 100));

Promise.race([promise1, promise2]).then(result => {
  console.log(result); // Will output "Second", as promise2 resolves faster
});