Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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);
    }
})();
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #Example #AggregateError
ADD COMMENT
Topic
Name
5+8 =