Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Example of Promise.any() and AggregateError in es12

// Create a Promise.
const promise1 = new Promise((resolve, reject) => {
    // After 2 seconds resolve the first promise.
    setTimeout(() => resolve("The first promise has been resolved."), 2000);
});

// Create a Promise.
const promise2 = new Promise((resolve, reject) => {
    // After 1 second resolve the second promise.
    setTimeout(() => resolve("The second promise has been resolved."), 1000);
});

// Create a Promise.
const promise3 = new Promise((resolve, reject) => {
    // After 3 seconds resolve the third promise.
    setTimeout(() => resolve("The third promise has been resolved."), 3000);
});

(async function () {
    const data = await Promise.any([promise1, promise2, promise3]);
    // Print the data returned from the first resolved Promise.
    console.log(data);
    // The above will print: The second promise has been resolved.
})();
Comment

Example of AggregateError in es12

// Create a Promise.
const promise1 = new Promise((resolve, reject) => {
    // After 1 seconds rejects the first promise.
    setTimeout(() => resolve("The first promise has been rejected."), 1000);
});

// Create a Promise.
const promise2 = new Promise((resolve, reject) => {
    // After 500 milliseconds rejects the second promise.
    setTimeout(() => resolve("The second promise has been rejected."), 500);
});

// Try executing the Promises.
(async function () {
    try {
        const data = await Promise.any([promise1, promise2]);
        console.log(data);
    } catch (error) {
        // If all Promises gets rejected, then this try-catch block will handle
        // the aggregate errors.
        console.log("Error: ", error);
    }
})();
Comment

PREVIOUS NEXT
Code Example
Javascript :: Example of Nullish coalescing assignment operator in es12 
Javascript :: join () method to join all elements of the array into a string to reverse an string 
Javascript :: Renaming props in react 
Javascript :: Adding Custom Admin Notices in the Classic Editor wordpress 
Javascript :: Implicit returns in ES6 
Javascript :: How to get element margin in React 
Javascript :: angular material primary lighter 
Javascript :: material ui table row onclick 
Javascript :: cypress contains regex 
Javascript :: angular material table generator 
Javascript :: iteration methods 
Javascript :: react native red Oval bubble 
Javascript :: javascript values 
Javascript :: reuse jquery angular 
Javascript :: programmatically change mongoose schema enum values 
Javascript :: how to print an array inside another array in react 
Javascript :: this 
Javascript :: video js ajax 
Javascript :: javascript get token from query string 
Javascript :: Uploading profile pic with react.js node.js express.js REST API to sqlite DB 
Javascript :: return multiple native element react native 
Javascript :: axios check if call is already running 
Javascript :: nodejs css cotent tipe 
Javascript :: jquery elements to json 
Javascript :: regular expressiong to indentify bible references in a sentence 
Javascript :: optional validation vuetify 
Javascript :: how to verify json format is valid 
Javascript :: how to use mixed quotes in a sentence in js 
Javascript :: shopify get values from settings_data.json 
Javascript :: rollup js global installation 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =