|
| 1 | +// let promiseOne = new Promise(function (resolve, reject) { |
| 2 | +// setTimeout(function () { |
| 3 | +// console.log("async task is complete"); |
| 4 | +// resolve(); |
| 5 | +// }, 1000); |
| 6 | +// }); |
| 7 | +// promiseOne.then(function () { |
| 8 | +// console.log("promise consumed"); |
| 9 | +// }); |
| 10 | + |
| 11 | +// new Promise(function (resolve, reject) { |
| 12 | +// setTimeout(function () { |
| 13 | +// console.log("async task is complete"); |
| 14 | +// resolve(); |
| 15 | +// }, 1000); |
| 16 | +// }).then(function () { |
| 17 | +// console.log("async 2 is complete"); |
| 18 | +// }); |
| 19 | + |
| 20 | +// const threePromise = new Promise(function (resolve, reject) { |
| 21 | +// setTimeout(function () { |
| 22 | +// console.log("three task is done"); |
| 23 | +// resolve({ name: "Ashish", email: "example@gmail.com" }); |
| 24 | +// }, 1000); |
| 25 | +// }); |
| 26 | + |
| 27 | +// threePromise.then(function(user){ |
| 28 | +// console.log(user) |
| 29 | +// }) |
| 30 | + |
| 31 | +// const forPromise = new Promise(function (resolve, reject) { |
| 32 | +// setTimeout(function () { |
| 33 | +// let error = false; // Simulating an error condition |
| 34 | +// console.log("Async task is complete"); |
| 35 | +// if (!error) { |
| 36 | +// resolve({ |
| 37 | +// userName: "Ashish Choudhary", |
| 38 | + |
| 39 | +// password: 123, |
| 40 | +// }); |
| 41 | +// } else { |
| 42 | +// reject("Error: Something went wrong"); |
| 43 | +// } |
| 44 | +// }); |
| 45 | +// }); |
| 46 | + |
| 47 | +// forPromise |
| 48 | +// .then((user) => { |
| 49 | +// return user.userName; |
| 50 | +// }) |
| 51 | +// .then((username) => { |
| 52 | +// console.log(username); |
| 53 | +// }) |
| 54 | +// .catch((error) => { |
| 55 | +// console.log(error); |
| 56 | +// }) |
| 57 | +// .finally(() => { |
| 58 | +// console.log("Promise is completed"); |
| 59 | +// }); |
| 60 | + |
| 61 | +// let promiseFive = new Promise((resolve, reject) => { |
| 62 | +// setTimeout(() => { |
| 63 | +// let error = false; |
| 64 | +// console.log("Async task is complete"); |
| 65 | +// if (!error) { |
| 66 | +// resolve({ |
| 67 | +// userName: "Java Script", |
| 68 | + |
| 69 | +// }); |
| 70 | +// } else { |
| 71 | +// reject("Error: JS went wrong"); |
| 72 | +// } |
| 73 | +// }, 1000); |
| 74 | +// }); |
| 75 | + |
| 76 | +// async function consumePromiseFive() { |
| 77 | +// const response = await promiseFive; |
| 78 | +// console.log(promiseFive); |
| 79 | +// } |
| 80 | + |
| 81 | +// consumePromiseFive() |
| 82 | + |
| 83 | +// api request |
| 84 | + |
| 85 | +async function getAllUsers() { |
| 86 | + try { |
| 87 | + const response = await fetch("https://randomuser.me/api/"); |
| 88 | + const data = await response.json(); |
| 89 | + console.log(data); |
| 90 | + } catch (error) { |
| 91 | + console.log("E", error); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// getAllUsers(); |
| 96 | + |
| 97 | +fetch("https://randomuser.me/api/") |
| 98 | + .then((response) => { |
| 99 | + return response.json(); |
| 100 | + }) |
| 101 | + .then((data) => { |
| 102 | + console.log(data); |
| 103 | + }) |
| 104 | + .catch((error) => console.log(error)); |
0 commit comments