You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 14, 2024. It is now read-only.
@@ -7,10 +7,12 @@ These are the topics for week 2:
7
7
1. JavaScript Versions
8
8
2. Promises
9
9
- Callback Hell
10
+
- Promise States
11
+
- Chaining
12
+
- Benefits
10
13
3. Arrow functions
11
14
- 'this' keyword
12
15
4. Fetch API
13
-
5. Thinking Like a Programmer III
14
16
15
17
## 1. JavaScript Versions
16
18
@@ -26,19 +28,22 @@ function aFunction() {
26
28
27
29
You did so using an old version of JavaScript.
28
30
29
-
But if you've ever used `arrow` functions (which you'll learn more about in the next section), you did so using a newer version of JavaScript.
31
+
But if you've ever used `Arrow Functions` (which you'll learn more about in the next section), you did so using a newer version of JavaScript.
30
32
31
-
That's good and all, but why is this important to differentiate? There are several reasons:
33
+
Each feature (and its updates) of a language is made to solve a specific problem. It's important to know the context and purpose of each to know how to use it.
32
34
33
-
- Each feature (and its updates) of a language is made to solve a specific problem. It's important to know the context and purpose of each to know how to use it
34
-
- Software is always evolving. This means that there are different versions that different users might be using. This means not every feature will work for every application.
35
+
Always be mindful of which **version** of any technology, library or language you are using. This is important, because this means that not everything will work the same way, even if it's the same tool you've been using! In your coding journey you'll come across many code bases that will include different versions of technologies.
35
36
36
-
That's why it's important to know a little about the history of JavaScript: it will make you think of JavaScript (and hopefully software in general) as a continually evolving thing, as opposed to "just a bunch of concepts and techniques you need to memorize".
37
+
Software is always evolving. This means that there are different versions that different users might be using. This means not every feature will work for every application.
38
+
39
+
Because of this evolutionary nature of software, it's important to know a little about the history of the language you're using. In our case this is JavaScript, and by knowing about its history this will help you think of it as a continually evolving thing, as opposed to "just a bunch of concepts and techniques you need to memorize".
37
40
38
41
Check the following resources out to learn more about this:
39
42
40
43
-[The History of JavaScript | Why is JavaScript also called ECMAScript?](https://www.youtube.com/watch?v=JpwxjkpZfhY)
41
44
-[The Weird History of JavaScript](https://www.youtube.com/watch?v=Sh6lK57Cuk4)
45
+
-[What Is ES6, ES2015, ES2016, ES2017 & ESNext](https://www.youtube.com/watch?v=9A_jkh2AKR8)
46
+
-[The Future of JavaScript - New Features and Disruptive Trends in 2020](https://www.youtube.com/watch?v=f0DrPLKf6Ro)
42
47
43
48
## 2. Promises
44
49
@@ -50,74 +55,165 @@ But what if you want to have callbacks within callbacks... within callbacks? Thi
50
55
51
56
-[Callback Hell](http://callbackhell.com/)
52
57
53
-
This is where `Promises` come in. The concept of a Promise, in execution, doesn't add anything new. It does exactly what callbacks aim to do, which is enabling asynchronous actions: for example, clicking a button to load in an image, while still being able to navigate the webpage.
58
+
This is where `Promises` come in. The idea of the `Promise` is a product of the evolution within the JavaScript language. A bunch of JavaScript developers wanted to figure out how to solve the problem of callback hell and this is what they came up with. Here's a basic example:
What a Promise does is make writing callbacks more readable for you, the developer. This is its main benefit. In effect, you could call Promises the updated version of callbacks. Callbacks version 2.0.
74
+
1. Pending: The asynchronous code is being executed, with no result yet
75
+
2. Fulfilled (resolved): The asynchronous code has successfully been executed without problems
76
+
3. Rejected: The asynchronous code has failed because of some error
77
+
78
+
When a Promises is executed it will first execute the asynchronous code inside. In this process it will go into the `pending` state. After, depending on if it succeeded or not, it will be `resolved` or `rejected`.
79
+
80
+
### Chaining
81
+
82
+
What if you need to perform several asynchronous operations, that depend on the result of the one that came before it? For that we can use the `.then()` method: a special function, given to us by the Promise object, that allows us to directly use the return value of the asynchronous operation that happened before. Here's an example:
83
+
84
+
```js
85
+
newPromise(function (resolve, reject) {
86
+
setTimeout(() =>resolve(1), 1000); // We wait 1 second and then resolve with value 1
87
+
})
88
+
.then(function (result) {
89
+
console.log(result); // Result: 1
90
+
return result *2;
91
+
})
92
+
.then(function (result) {
93
+
alert(result); // Result: 2
94
+
return result *2;
95
+
})
96
+
.catch((error) => {
97
+
console.log(error);
98
+
});
99
+
```
100
+
101
+
By chaining the Promise we can gain greater control over the results of our asynchronous operations!
102
+
103
+
### Benefits
104
+
105
+
The concept of a Promise, in execution, doesn't add anything new. It does exactly what callbacks aim to do, which is enabling `asynchronous actions`: for example, clicking a button to load in an image, while still being able to navigate the webpage.
106
+
107
+
So what are the benefits of using a Promise over a callback? Here's a few:
108
+
109
+
1. It makes writing asynchronous JavaScript code more readable for you, the developer. In effect, you could call Promises the updated version of callbacks. Callbacks version 2.0.
110
+
2. Better error handling, because of the `catch` block attached at the end. When something goes wrong within the Promise structure, it will throw an error. This error will then be caught and handled within the `catch` block.
56
111
57
112
Go through the following resources to learn more about Promises:
58
113
114
+
-[JavaScript Promises for Dummies](https://scotch.io/tutorials/javascript-promises-for-dummies)
One of a programmer's favorite things to do is to write clean and concise code. Arrow functions are a new way to write functions, given to us by the ECMAScript 6 (The software standard JavaScript is based upon) update of JavaScript.
121
+
One of a programmer's favorite things to do is to write clean and concise code. `Arrow Functions` are a new way to write functions, given to us by the ECMAScript 6 (The software standard JavaScript is based upon) update of JavaScript.
66
122
67
-
It is written like this:
123
+
It's a little different from regular functions, take a look:
68
124
69
125
```js
70
-
constnumbers= [2, 3, 7, 8];
71
-
72
-
//classical way
73
-
functionisBiggerThanFive(number) {
74
-
if (number >5) returntrue;
75
-
elsereturnfalse;
126
+
// ES5 Function
127
+
functionaddNum(num1, num2) {
128
+
return num1 + num2;
76
129
}
77
-
console.log(numbers.filter(isBiggerThanFive));
78
-
79
-
// Arrow function
80
-
console.log(
81
-
numbers.filter((number) => {
82
-
if (number >5) returntrue;
83
-
elsereturnfalse;
84
-
}),
85
-
);
86
-
87
-
// or even shorter, if one arguments no need for (), if return statement no need for {}
88
-
console.log(numbers.filter((number) => number >5));
130
+
131
+
// Arrow Function (stored in variable)
132
+
constaddNum= (num1, num2) => {
133
+
return num1 + num2;
134
+
};
89
135
```
90
136
91
-
Go through the following resources to learn more about why arrow functions are important:
137
+
If you've done some research, you may come to the following conclusions:
138
+
139
+
1. First of all, the Arrow Function is anonymous by design. If we want to refer to it, we should store it into a variable.
140
+
2. Secondly, the way Arrow Functions can be written can differ (while still working the same way). Sometimes you don't need the `()` if there's a single or no parameters. Sometimes you can `return` a value without use for the `return` keyword.
141
+
142
+
Another big important feature of Arrow Functions (and difference with ES5 functions) is the way they relate to the `this` keyword: instead of creating a new `this` object, it actually inherits it from the parent scope!
143
+
144
+
If this sounds incomprehensible still, not to worry. In the next section will dive deep into the `this` keyword: what it means and how we can make use of it.
145
+
146
+
For now, go through the following resources to learn more about why arrow functions are important:
-[When (and why) you should use ES6 arrow functions — and when you shouldn’t](https://www.freecodecamp.org/news/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26/)
96
151
97
152
### The `this` keyword
98
153
99
-
In JavaScript, like in any other programming language you'll find, there are certain special keywords that always create a specific effect. The `this` keyword is one of those.
154
+
In everyday communication, we use words like "this" or "that" whenever we want to refer to things in the world or something someone said. It's similarly used in JavaScript, only instead of real-world things we refer to objects.
155
+
156
+
In JavaScript `this` refers to any object it's defined in. The global object, `window` is the default value of `this`. So if you go to your console right now and type `this`, you'll get back a reference to the `window` object. The same thing would happen if you to log `this` inside of your JavaScript file:
157
+
158
+
```js
159
+
console.log('what is', this);
160
+
```
161
+
162
+
However, this isn't the only value `this` can have. The moment we create a new object, we create a new `this` keyword that refers to only that object.
163
+
164
+
```js
165
+
constwouter= {
166
+
firstName:'Wouter',
167
+
lastName:'Kleijn',
168
+
getFullName:function () {
169
+
returnthis.firstName+''+this.lastName;
170
+
},
171
+
};
172
+
```
173
+
174
+
In this example `this` refers to the complete `wouter` object. If we execute `wouter.getFullName()`, we get back the value of `wouter.firstName` and `wouter.lastName`.
100
175
101
-
In everyday communication, we use words like "this" or "that" whenever we want to refer to things in the world or something someone said. It's similarly used in JavaScript.
176
+
```js
177
+
wouter.getFullName; // Result: Wouter Kleijn
178
+
```
102
179
103
-
Simply put: `this` refers to any object it's defined in. The global object, `window` is the default value of `this`. However, anything a new object is created will have its own `this` value.
180
+
As you can imagine, this means that there can be multiple `this` keywords at play: the global `this` keyword (which refers to the `window` object) and a `this` keyword for every object that is created within the application.
104
181
105
182
Go through the following resources to learn more about `this`:
106
183
107
-
-[Understanding "this" in JavaScript](https://www.codementor.io/dariogarciamoya/understanding--this--in-javascript-du1084lyn)
184
+
-[What is THIS in JavaScript? in 100 seconds](https://www.youtube.com/watch?v=YOlr79NaAtQ)
-[Understanding "this" in JavaScript](https://www.codementor.io/dariogarciamoya/understanding--this--in-javascript-du1084lyn)
109
187
110
188
## 4. Fetch API
111
189
112
-
Last week you learned about making HTTP Requests. You learned how to do this using the XHR object, which we can access through the browser's `window` object.
190
+
Last week you learned about making HTTP Requests. You learned how to do this using the `XHR` object, which we can access through the browser's `window` object.
113
191
114
192
Now as we've learned in the previous sections, JavaScript as a language evolves continually. But so do browsers! New features get added to increase the user experience and make life easier for developers.
115
193
116
-
One of those features added to browsers is an upgraded version of the XHR object. It's called `fetch` and it's the modern way to make HTTP Requests. It incorporates Promises, making it easier to handle your server responses.
194
+
One of those features added to browsers is an upgraded version of the XHR object. It's called the `Fetch API` and it's the modern way to making HTTP Requests. It incorporates Promises, making it easier to handle your server responses. Here's a basic example:
195
+
196
+
```js
197
+
fetch('https://pokeapi.co/api/v2/pokemon')
198
+
.then((response) => {
199
+
returnresponse.json();
200
+
})
201
+
.then((data) => {
202
+
console.log('Pokemon data', data);
203
+
return data;
204
+
})
205
+
.catch((error) => {
206
+
console.log('err', error);
207
+
});
208
+
```
209
+
210
+
Where is this function defined, you may wonder? Simple: it's found in the global `window` object in the browser. You can check it out by opening your console in the browser. Type in `fetch` and you'll get back a function definition.
211
+
212
+
When your JavaScript file is loaded into the DOM, it automatically will have access to any of the browser's web APIs (one if which is the `Fetch API`). That's why you can use it in your JavaScript files.
117
213
118
-
A `fetch` function is now provided in the global `window` scope in the browser. You can check it out by opening your developers tools and searching for `fetch`. Keep in mind that `fetch` only works on newer browser version. To figure out which browsers can use fetch, check [this](https://caniuse.com/#feat=fetch) out.
214
+
> Keep in mind that `fetch` only works on newer browser versions. To figure out which browsers can use fetch, check [this](https://caniuse.com/#feat=fetch) out.
119
215
120
-
Learn more about `fetch`:
216
+
To learn more and practice with the `Fetch API`, check out the following:
121
217
122
218
-[Fetch API Introduction](https://www.youtube.com/watch?v=Oive66jrwBs)
123
219
-[Sending JavaScript Http Requests with the fetch() API](https://www.youtube.com/watch?v=23hrM4saaMk)
0 commit comments