
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How does Promise.any() method differs from Promise.race() method in JavaScript?
In this article, you will understand how Promise.any() method differs from Promise.race() method in JavaScript.
The Promise.any() method in javascript is one among promise concurrency methods. It is useful when the first task needs to be completed.
The Promise.race() method in javascript is one among promise concurrency methods. It is useful when the first async task need to be complete, but do not care about its eventual state (i.e. it can either succeed or fail).
Example 1
In this example, let’s look at how the Promise.any() method works
console.log("Defining three promise values: promise1, promise2 and promise3"); const promise1 = Promise.resolve(1); const promise2 = new Promise((resolve, reject) => { setTimeout(resolve, 2 , 'Promise Two'); }); const promise3 = 3; console.log("
Running Promise.any method on all the three promise values") Promise.any([promise1, promise2, promise3]).then((values) => console.log(values));
Explanation
Step 1 − Define three promise values namely promise1, promise2, promise3 and add values to them.
Step 2 − Run Promise.any() method on all the promise values.
Step 3 − Display the promise values as result.
Example 2
In this example, let’s look at how the Promise.race() method works
console.log("Defining three promise values: promise1, promise2 and promise3"); const promise1 = Promise.resolve(Resolving first async promise); const promise2 = new Promise((resolve, reject) => { setTimeout(resolve, 2 , 'Promise Two'); }); const promise3 = 3; console.log("
Running Promise.race method on all the three promise values") Promise.race([promise1, promise2, promise3]).then((values) => console.log(values));
Explanation
Step 1 − Define three promise values namely promise1, promise2, promise3 and add values to them.
Step 2 − Run Promise.race() method on all the promise values.
Step 3 − Display the promise values as result.
- Related Articles
- How does Promise.all() method differs from Promise.allSettled() method in JavaScript?
- How does JavaScript focus() method works?
- JavaScript Array from() Method
- Explain Promise.any() with async-await in JavaScript?
- Explain Promise.race() with async-await in JavaScript?
- How does jQuery.clone() method work in jQuery?
- How does jQuery.filter() method work in jQuery?
- How does jQuery.find() method work in jQuery?
- How does jQuery.eq() method work in jQuery?
- How does jQuery.map() method work in jQuery?
- How does jQuery.slice() method work in jQuery?
- How does jQuery.nextAll() method work in jQuery?
- How does jQuery replaceWith() method work?
- How does constructor method __init__ work in Python?
- How does the series.copy() method work in Pandas?
