-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (105 loc) · 3.62 KB
/
index.js
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
// Declaring variables
let name = 'Pepe';
// code
// code
name = 'Jose';
const numbers = [1, 2, 3];
// arrow functions
const reverseWord = (word) => word.split('').reverse().join('');
const isPaul = (word) => word === 'Paul';
// normal functions declared as constants
const reverseEachWord = function (sentence) {
return sentence
.split(' ')
.map((word) => reverseWord(word))
.join(' ');
};
// normal functions
function sumNumbers(num1, num2) {
return num1 + num2;
}
// Arrays
const users = ['Paul', 'Abraham', 'Paul', 'Octavio', 'Marco'];
users.push('Octavio');
users.forEach((user) => console.log(user));
console.log('-----------------');
console.log('Reverse a Sentence: "This is a test"');
console.log(users.map((user) => reverseWord(user)));
// ---------------------------------------------
console.log('-----------------');
console.log(users.some((user) => isPaul(user)));
console.log(users.every((user) => user === 'Octavio'));
console.log(users.filter((user) => isPaul(user)));
console.log(`The found value is: ${users.find((user) => user === 'Octavio')}`); // String interpolation
const printTrue = (statement) => console.log(`${statement} statement => OK condition is TRUE`);
const printFalse = (statement) => console.log(`${statement} statement => OK condition is FALSE`);
console.log('-----------------');
// if - else statement
const condition = false;
if (condition) {
printTrue('if-else');
} else {
printFalse('if-else');
}
console.log('-----------------');
// ternary operator
condition ? printTrue('ternary-operator') : printFalse('ternary-operator');
console.log('-----------------');
console.log(`truthy/falsy: ${0 || 'default'}`); // truthy/falsy
console.log(`coalescing: ${0 ?? 'default'}`); // coalescing operator not null/ not undefined or default
console.log('-----------------');
// ------------------------------------------------- o ------------------------------------------------
// Promises
/* Promises in JS are like promises in real life, you either
keep your word or you break the promise
in JS
- keep my word => promise is resolved
- break my promise => promise is rejected
- truthy !== undefined, null, false
*/
let value = true;
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => (value ? resolve("Good, it's resolved") : reject('Too bad')), 5000);
// if (value) resolve("Good, it's resolved");
// else reject('Too bad');
});
const myPromise1 = new Promise((resolve, reject) => {
setTimeout(() => (value ? resolve("Good, it's resolved") : reject('Too bad')), 5000);
// if (value) resolve("Good, it's resolved");
// else reject('Too bad');
});
Promise.all([myPromise, myPromise1]).then((responses) => console.log(responses));
Promise.race([myPromise, myPromise1]).then();
value = true;
console.log('test');
myPromise.then((response) => {
let res = response;
if (res == 'one string') console.log(res);
});
// ES6 async / await
const myAsyncPromise = async () => {
return new Promise((resolve, reject) => {
setTimeout(() => (value ? resolve("Good, it's resolved") : reject('Too bad')), 5000);
});
};
const resolvePromise = async () => {
let myVal = await myAsyncPromise();
console.log(myVal);
true ? console.log('y') : console.log('f');
};
resolvePromise();
// assumiung we have PO ==> async /await
//Login page
async function login(username, password) {
return new Dashboard();
}
const dashboard = await login();
await dashboard.showCritical();
// .then
function login(username, password) {
return new Promise((resolve) => resolve(new Dashboard()));
}
let val = null;
login().then((dashboard) => {
dashboard.showCritical().then((criticals) => criticals.then());
});