JS Coding Questions Logo
JS Coding Questions
#66💼 Interview💻 Code

What is the purpose of the race method in promise

Advertisement

728x90

Promise.race() method will return the promise instance which is firstly resolved or rejected. Let's take an example of race() method where promise2 is resolved first

javascript
1var promise1 = new Promise(function (resolve, reject) {
2
3setTimeout(resolve, 500, "one");
4});
5
6var promise2 = new Promise(function (resolve, reject) {
7
8setTimeout(resolve, 100, "two");
9});
10
11Promise.race([promise1, promise2]).then(function (value) {
12
13console.log(value); // "two" // Both promises will resolve, but promise2 is faster
14});

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 67

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
66of476