-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL7.js
More file actions
210 lines (177 loc) · 4.01 KB
/
L7.js
File metadata and controls
210 lines (177 loc) · 4.01 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class User{
constructor (name, age) {
this.name = name;
this.age = age;
}
getDetails() {
// simulating Async server call
setTimeout(function () {
this.details = [10, 20, 30]; //but "this" will loose its context
}, 2000);
}
}
// We already saw two ways of handling this situation.
// 1. Closure for saving context -> var self = this;
// 2. Using bind() method
// Fat arrow function -> anonymous function
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
getDetails() {
// simulating Async server call
setTimeout(() => {
this.details = [10, 20, 30]; // javascript will bind the "this" to the fat arrow function
}, 2000);
}
}
// Dont spam the fat arrow function -> it store the context
// backtracking and debugging is hard
// We can provide name by this way:
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
getDetails() {
var getDetailsTimeout = () => {
this.details = [10, 20, 30];
}
setTimeout(getDetailsTimeout, 2000);
}
}
// Object literal shorthand syntax
var data = [10, 20, 30];
var num = 100;
var obj = {
name: "John",
age: 30
};
var newObj = {
obj, // same as obj: obj
data, // same as data: data
num // same as num: num
};
// var obj = {
// num: 10
// };
// console.log(obj.num); // 10 -> dot notation
// console.log(obj["num"]); // 10 -> bracket/Array/index notation
// var obj2 = {};
// obj2[Math.random()] = 20;
var data = [10, 20, 30];
var data2 = data.map(
function (item, index) {
var obj = {};
obj["element_" + index] = item; //computed property
return obj;
}
);
var data3 = {};
data.forEach(
function (item, index) {
data3["element_" + index] = item;
}
);
console.log(data3);
// signaling
function f1(cb) {
console.log("f1");
setTimeout(function () {
cb(true);
}, 1000);
}
function f2() {
console.log("f2");
function cb(isSuccess) {
if (isSuccess) {
console.log("f2 executed after f1");
} else {
console.log("failed");
}
}
}
// Node js do like this
// Another way:
function f1(success, error) {
console.log("f1");
setTimeout(function () {
success();
}, 1000);
}
function f2() {
console.log("f2");
f1(
function success() {
console.log("f2 executed after f1");
},
function error() {
console.log("failed");
}
);
}
// third version: ES6 promise
function f1() {
var promise = new Promise(
function onThen(resolve, reject) { // when then is called this will get fired
setTimeout(
function () {
console.log("f1");
resolve();
}, 1000
)
}
);
return promise;
}
function f2() {
console.log("f2 before f1");
var promise = f1();
f3(promise);
}
function f3(obj) {
promise.then( // wait till f1 is done
function success() {
console.log("f2 executed after f1");
},
function error() {
console.log("failed");
}
);
}
// Async/Await
async function f2() {
console.log("f2 before f1");
try {
await f1();
console.log("f2 executed after f1");
}
catch (err) {
console.log("failed");
}
}
// HTTP Browser - HTTP call
// HTTP/HTTPS
// WS - WebSocket WSS - Secure WebSocket
function callApi() {
var xhr = new XMLHttpRequest();
var apiEndpoint = "";
xhr.open(
"GET", //HTTP GET
apiEndpoint, // URL
true // async
);
var obj = {
name: "John",
age: 30
}
var objJson = JSON.stringify(obj);
xhr.onload = function () { //success
var data = xhr.response;
console.log(data);
}
xhr.onerror = function () { //error
}
xhr.send(objJson); // send the data
}