forked from ironhack-labs/lab-es6-promises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (127 loc) · 4.6 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
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
function printString(str){
setTimeout(() => {
console.log(str);
}, Math.floor(Math.random() * 2000));
}
function printAll(){
printString("A");
printString("B");
printString("C");
}
//printAll();
// You will notice that A, B, and C print in a different and random order each time you call printAll!
// This is because these functions are asynchronous. Each function gets executed in order, but each one is
// independent with it’s own setTimeout. They won’t wait for the last function to finish before they start.
// CALLBACKS
// A callback is a function that is passed to another function.
// When the first function is done, it will run the second function.
function printStringWithCallbacks(str, callback){
setTimeout(() => {
console.log(str);
callback();
}, Math.floor(Math.random() * 2000));
}
function printAllWithCallbacks(){ //callbacks
printStringWithCallbacks("A", function(){
printStringWithCallbacks("B", function(){
printStringWithCallbacks("C", function(){
console.log("hello");
})
})
})
}
//printAllWithCallbacks()
// Problem with callbacks: it creates something called “Callback Hell.”
// solution: Promises - here to fix this nesting problem.
// PROMISES
function printStringWithPromises(str){
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(str);
resolve();
}, Math.floor(Math.random() * 2000));
});
}
function printAllWithPromises(){
printStringWithPromises("A")
.then(()=>console.log('get'))
.then(() => printStringWithPromises("B"))
.then(() => printStringWithPromises("C"))
.then(() => console.log('almost there'))
.catch(() => console.log("Something bad happened!"));
}
//printAllWithPromises()
// ASYNC/AWAIT
// ASYNC/AWAIT
// Await is basically syntactic sugar for Promises. It makes your asynchronous code look more like synchronous/procedural code,
// which is easier for humans to understand.
// The printStringWithPromises function doesn’t change at all from the promise version.
async function printAllWithAsync(){
await printStringWithPromises("A");
await printStringWithPromises("B");
await printStringWithPromises("C");
}
async function useAxios() {
let vitalii = await axios.get('https://ironrest.herokuapp.com/vitalii')
let pradeepa = await axios.get('https://ironrest.herokuapp.com/pradeepa')
let everybody = await axios.get('https://ironrest.herokuapp.com')
let promise = new Promise((resolve, reject)=>{
setTimeout(()=>{
resolve('horrayyyYY!')
},5000)
})
Promise.all([vitalii, pradeepa, everybody, promise]).then(data=>{
console.log('in here',data)
})
}
//useAxios()
axios.get('https://ironrest.herokuapp.com/vitalii').then(data=>{
console.log(data)
})
//printAllWithAsync()
// The printString function doesn’t return anything and is independent, all we cared about was the order.
// Take the output of the first function, do Something with it in the second function, and then pass it to the third function.
// Instead of printing the string each time, let’s make a function that will concatenate the string and pass it on.
function addString(str1, str2, callback){
setTimeout(() => {
callback(`${str1} ${str2}`);
}, Math.floor(Math.random() * 100)
);
}
function addAll(){
// result is callback
addString('', 'A', result => {
addString(result, 'B', result => {
addString(result, 'C', result => {
console.log(result); // Prints out " A B C"
});
});
});
}
// addAll()
function addStringWithPromises(str1, str2){
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`${str1} ${str2}`);
},
Math.floor(Math.random() * 100)
);
});
}
function addAllWithPromise(){
addStringWithPromises('', 'A')
.then(result => addStringWithPromises(result, 'B'))
.then(result => addStringWithPromises(result, 'C'))
.then(result => console.log(result));
// Prints out " A B C"
}
// addAllWithPromise()
async function addAllWithPromise(){
let toPrint = ''
toPrint = await addStringWithPromises(toPrint, 'A');
toPrint = await addStringWithPromises(toPrint, 'B');
toPrint = await addStringWithPromises(toPrint, 'C');
console.log(toPrint); // Prints out " A B C"
}
//addAllWithPromise();
// Additional read: https://medium.com/javascript-in-plain-english/a-guide-to-javascript-promises-da50eff327d7