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 Private Class Methods and Accessors in es12 
Javascript :: Mapping page number to index 
Javascript :: Example to adds two colour palettes to the sidebar in wordpress 
Javascript :: Adding Notices in the Block Editor Wordpress 
Javascript :: Key and property shorthand in ES6 
Javascript :: blur area behind an element in react native 
Javascript :: used as a function, Number() will convert another value 
Javascript :: Async functions and execution order 
Javascript :: populating selectfield from json file 
Javascript :: Determining Truth With Logical Operators 
Javascript :: Nested Data Structures 
Javascript :: react native red Triangle Down 
Javascript :: Create an Array of specific length with some value at each index 
Javascript :: js shufflin 
Javascript :: react redux open another page 
Javascript :: x is not a function javascript type error 
Javascript :: normalizedList.flatMap is not a function vue draggable 
Javascript :: peopleToSendMessage 
Javascript :: Line logger 
Javascript :: how concatenate arrays in es6 
Javascript :: vue js key modifiers 
Javascript :: moment js days ago 
Javascript :: Webpack: How to compile, write on disk and serve static content (js/css/html/assets) using webpack-dev-server 
Javascript :: likedislike 
Javascript :: js how to remove blue while click 
Javascript :: vanilla js for each element add attribute 
Javascript :: Scratch Addon userscript 
Javascript :: zxaas 
Javascript :: trie node pseudocode 
Javascript :: get-the-current-directory-name-in-javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =