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