|
| 1 | +// console.log("is js working"); |
| 2 | + |
| 3 | +// fetch("https://something.com").then().catch().finally(); //Basic syntax |
| 4 | + |
| 5 | +// const promiseOne = new Promise(function (resolve, reject) { |
| 6 | +// //Do an async task |
| 7 | +// //DB calls ..conntect..send..recive ..inject CryptoGraphy Network Call |
| 8 | + |
| 9 | +// setTimeout(function () { |
| 10 | +// console.log("async task is completed "); |
| 11 | +// resolve(); |
| 12 | +// }, 2000); |
| 13 | +// }); |
| 14 | +// promiseOne.then(function () { |
| 15 | +// console.log("process is consumed"); |
| 16 | +// }); |
| 17 | + |
| 18 | +// new Promise(function (resolve, reject) { |
| 19 | +// setTimeout(function () { |
| 20 | +// console.log("async task 2"); |
| 21 | +// resolve(); |
| 22 | +// }, 1000); |
| 23 | +// }).then(function () { |
| 24 | +// console.log("async task 2 is resolved"); |
| 25 | +// }); |
| 26 | + |
| 27 | +// const promiseThree = new Promise(function (resolve, reject) { |
| 28 | +// setTimeout(function () { |
| 29 | +// resolve({ |
| 30 | +// name: "waseem akram", |
| 31 | +// gmail: "malikwaseemshzad@gmail.com", |
| 32 | +// }); // in resolve parameter we can pass object ,fucntion and array .... |
| 33 | +// }, 1000); |
| 34 | +// }); |
| 35 | + |
| 36 | +// promiseThree.then(function (user) { |
| 37 | +// // console.log(user); |
| 38 | +// // console.log(user.gmail); |
| 39 | +// }); |
| 40 | + |
| 41 | +// const promiseFour = new Promise(function (resolve, reject) { |
| 42 | +// setTimeout(function () { |
| 43 | +// let error = false; |
| 44 | +// if (!error) { |
| 45 | +// resolve({ username: "wcoder547", password: "123" }); |
| 46 | +// } |
| 47 | +// if (error) { |
| 48 | +// reject("Error,Something Went Wrong!!"); |
| 49 | +// } |
| 50 | +// }, 1000); |
| 51 | +// }); |
| 52 | + |
| 53 | +// promiseFour |
| 54 | +// .then(function (user) { |
| 55 | +// console.log(user); |
| 56 | +// return user.username; |
| 57 | +// }) |
| 58 | +// .then((username) => { |
| 59 | +// console.log(username); |
| 60 | +// }) |
| 61 | +// .catch(function (error) { |
| 62 | +// console.log(error); |
| 63 | +// }) |
| 64 | +// .finally(() => console.log("The promise is resolved or rejected")); |
| 65 | + |
| 66 | +// const promiseFive = new Promise(function (resolve, reject) { |
| 67 | +// setTimeout(function () { |
| 68 | +// let error = true; |
| 69 | +// if (!error) { |
| 70 | +// resolve({ username: "wcoder547", password: "123" }); |
| 71 | +// } |
| 72 | +// if (error) { |
| 73 | +// reject("Error,Something Went Wrong!!"); |
| 74 | +// } |
| 75 | +// }, 1000); |
| 76 | +// }); |
| 77 | + |
| 78 | +// async function consumePromiseFive() { |
| 79 | +// try { |
| 80 | +// const response = await promiseFive; |
| 81 | +// console.log(response); |
| 82 | +// } catch (error) { |
| 83 | +// console.log(error); |
| 84 | +// } |
| 85 | +// } |
| 86 | + |
| 87 | +// consumePromiseFive(); |
| 88 | + |
| 89 | +// async function getAllUsers() { |
| 90 | +// try { |
| 91 | +// const response = await fetch("https://jsonplaceholder.typicode.com/users"); |
| 92 | + |
| 93 | +// const data = await response.json(); |
| 94 | +// console.log(data); |
| 95 | +// } catch (error) { |
| 96 | +// console.log(error); |
| 97 | +// } |
| 98 | +// } |
| 99 | +//getAllUsers(); |
| 100 | + |
| 101 | +fetch("https://jsonplaceholder.typicode.com/users") |
| 102 | + .then(function (response) { |
| 103 | + return response.json(); |
| 104 | + }) |
| 105 | + .then(function (data) { |
| 106 | + console.log(data); |
| 107 | + }) |
| 108 | + .catch(function (error) { |
| 109 | + console.log(error); |
| 110 | + }); |
0 commit comments