Explain Promise.race() with async-await in JavaScript
Last Updated :
09 Jan, 2023
In this article, we will try to understand how we may implement Promise.race() method with async-await in JavaScript with the help of certain coding examples as well as theoretical explanations. Let us first quickly understand how we may implement Promise.race() method. This method is one of the most commonly used promise methods that state whichever promise will reach its end, first will be executed first and the rest will remain not executed. This may also be realized in real terms as whoever will reach first in the race will win the race, here also the concept remains the same.
Syntax: The following syntax will help us to understand how we may implement Promise.race() method:
Promise.race([first_promise , second_promise, ...]).then(
// do something......
)
Let us see the following example for a better understanding of the above-illustrated Promise.race() method's syntax:
Example 1: In this example, we will be creating three promises one after another with a timer function (setTimeout() method) inside each one of them. Each timer function would have different timings which will be mentioned at the end of their illustration. Then we will use Promise.race() method which will take all three promises as input in the form of an array and executes them as per its own role.
JavaScript
let first_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("GeeksforGeeks...!!");
}, 1000);
});
let second_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("JavaScript......!!");
}, 2000);
});
let third_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("TypeScript...!!");
}, 3000);
});
let promise_array = [first_promise, second_promise, third_promise];
Promise.race(promise_array).then((result) => console.log(result));
Output:
GeeksforGeeks...!!
Now that we have understood how we may execute Promise.race() method, let us see the following example which will help us to understand how we may implement Promise.race() method with async-await keywords.
Example 2: Like in the previous example, here we will be creating three promises, each having different timer functions with different timeouts. Then we will create a function (or a method) having prefix as async and inside it, we will store the result using another keyword called await then we will display the result with the help of then() method.
JavaScript
let first_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("GeeksforGeeks...!!");
}, 1000);
});
let second_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("JavaScript......!!");
}, 2000);
});
let third_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("TypeScript...!!");
}, 3000);
});
async function displayResult() {
let promise_array = [first_promise, second_promise, third_promise];
let result = await promise_array;
Promise.race(promise_array).then((result) => console.log(result));
}
displayResult();
Output:
GeeksforGeeks...!!
Similar Reads
Explain Promise.any() with async-await in JavaScript In this article, we will try to understand how to implement the Promise.any() method with async-await in JavaScript using some theoretical explanations followed by some coding examples as well. Let us firstly quickly understand the working of Promise.any() method with some theoretical examples (incl
4 min read
Explain Promise.all with async-await in JavaScript In JavaScript, Promise.all with async-await is used to handle multiple asynchronous operations concurrently. By combining Promise.all with await, you can wait for all promises to resolve or any to reject, ensuring that all asynchronous tasks are complete before proceeding with the next code executio
4 min read
Explain Promise.allSettled() with async-await in JavaScript In this article, we will try to understand how we may implement the Promise.allSettled() method with async-await in JavaScript with the help of certain coding examples as well as theoretical explanations. Let us first quickly understand about Promise.allSettled() method. This method is one of the mo
3 min read
Explain Async Await with Promises in Node.js Async/Await in Node.js is a powerful way to handle asynchronous operations. It allows you to write asynchronous code in a synchronous manner, making it easier to read, write, and debug. This feature is built on top of Promises, which represent a value that may be available now, or in the future, or
4 min read
How to delay a loop in JavaScript using async/await with Promise ? In JavaScript, you can delay a loop by using async/await with Promise. By wrapping a setTimeout() inside a Promise and using await, you can pause execution at each iteration, creating delays between loop iterations for asynchronous tasks without blocking the main thread.What is async and await?async
2 min read
How to use async/await with forEach loop in JavaScript ? Asynchronous is popular nowadays because it gives functionality of allowing multiple tasks to be executed at the same time (simultaneously) which helps to increase the productivity and efficiency of code. Async/await is used to write asynchronous code. In JavaScript, we use the looping technique to
2 min read