Promise.all Method tutorial in Advance JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Promise.all Method Advance JavaScript</title>
<link rel="stylesheet" href="">
</head>
<body>
<script>
//Example 1 :
// let p1 = new Promise((resolve, reject) => {
// setTimeout(() => {
// console.log("The First promise has resolved");
// resolve(10);
// }, 1 * 1000);
// });
// let p2 = new Promise((resolve, reject) => {
// setTimeout(() => {
// console.log("The Second promise has resolved");
// resolve(20);
// }, 2 * 1000);
// });
// let p3 = new Promise((resolve, reject) => {
// setTimeout(() => {
// console.log("The Third promise has resolved");
// resolve(30);
// }, 3 * 1000);
// });
// var total = 0;
// Promise.all([p1, p2, p3]).then((result) => {
// for (var i in result) {
// total += result[i];
// }
// console.log(`Result : ${result}`);
// console.log(`Total : ${total}`);
// }).catch((error) => {
// console.log(`Error : ${error}`);
// });
//Example 2 :
let promiseCall = function (returnData, message) {
return function (resolve, reject) {
setTimeout(() => {
console.log(`The ${message} promise has resolved`);
}, returnData * 100);
};
};
let p4 = new Promise(promiseCall(10, "First Call"));
let p5 = new Promise(promiseCall(20, "Second Call"));
let p6 = new Promise(promiseCall(30, "Third Call"));
let p7 = new Promise(function (resolve, reject) {
reject("The 4rth Promise has rejected.");
});
var total = 0;
Promise.all([p4, p5, p6, p7]).then((result) => {
for (var i in result) {
total += result[i];
}
console.log(`Result : ${result}`);
console.log(`Total : ${total}`);
}).catch((error) => {
console.log(`Error : ${error}`);
});
</script>
</body>
.png)
.png)
.png)
Comments
Post a Comment