-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
164 lines (132 loc) · 3.55 KB
/
Copy pathapp.js
File metadata and controls
164 lines (132 loc) · 3.55 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Synchoronous Behaviour is that code executed line by line
// Asynchoronous Behaviour
// async makes a function return a Promise
// await makes a function wait for a Promise
// SetTimeOut
// Fetch get data from link
// Promise :
// "I Promise a Result!"
// "Producing code" is code that can take some time
// "Consuming code" is code that must wait for the result
// A Promise is an Object that links Producing code and Consuming code
// JavaScript Promise Object
// A Promise contains both the producing code and calls to the consuming code:
var a = 'A'
var b = 'B'
// setTimeout(()=>{
// console.log(b);
// },2000)
// console.log(a);
// CallBack
// I will call back later!"
// A callback is a function passed as an argument to another function
// This technique allows a function to call another function
// A callback function can run after another function has finished
// fetch("https://api.escuelajs.co/api/v1/products")
// .then((res) => res.json())
// .then((data) => console.log("data==>", data));
// console.log(b);
//callbacks
//A callback is a function passed as an argument to another function
// function login(callback) {
// setTimeout(() => {
// console.log("User login");
// callback();
// }, 1000);
// }
// function getUserData() {
// let userInfo = {
// fullName: "Bilal Raza",
// };
// setTimeout(() => {
// console.log(userInfo);
// }, 500);
// }
// login(getUserData);
// let productsArr = [];
// function getProductsFromApi(callBack) {
// fetch("https://api.escuelajs.co/api/v1/products")
// .then((res) => res.json())
// .then((products) => {
// console.log("products==>", products);
// callBack(products);
// });
// }
// function displayProducts(products) {
// console.log("See all products", products);
// }
// getProductsFromApi(displayProducts);
// let token = "";
// function login(callback) {
// setTimeout(() => {
// console.log("User is Logged In");
// token = "ABCD";
// callback();
// }, 300);
// }
// function getUserData() {
// setTimeout(() => {
// console.log("token=>", token);
// if (token) {
// const user = {
// fullName: "Bilal Raza",
// };
// return user;
// }
// }, 1000);
// }
// login(getUserData);
// getUserData();
// let isValid = false;
// const p = new Promise((resolve, reject) => {
// if (isValid) {
// resolve("kaam hogya");
// } else {
// reject("Kaam aapka nahn hosaka");
// }
// });
// p.then((data) => console.log(data))
// .catch((err) => console.log("error=>", err));
// let isValid = false;
// function login() {
// return new Promise((resolve, reject) => {
// setTimeout(() => {
// if (isValid) {
// resolve("ABCD");
// } else {
// reject("Server is down");
// }
// }, 500);
// });
// }
// login()
// .then((token) => {
// console.log("User is logged In");
// getUserData(token);
// })
// .catch((err) => console.error(err));
// function getUserData() {
// setTimeout(() => {
// const user = {
// fullName: "Bilal Raza",
// };
// console.log(user)
// return user;
// }, 1000);
// }
let products = [];
fetch("https://fakestoreapi.com/products")
.then((res) => res.json())
.then((data) => {
products = data;
displayProducts();
}).catch((err)=> console.log(err));
function displayProducts() {
let list = document.getElementById("list");
products.forEach((data) => {
let li = `<li>${data.title} ($${data.price})</li>`;
list.innerHTML += li;
});
}
//A = ARRAY WITH PRODUCTS
//B = EMPTY ARRAY