Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3970aca

Browse files
author
Noer Paanakker
committed
week 3 reading update
1 parent 312fc90 commit 3970aca

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

Week1/MAKEME.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ The returned JSON data contains some basic information about each repository, su
136136

137137
Do the following to fulfill this week's requirements:
138138

139-
1. Modify **`imdex.js`**: Modify whatever you need in order to successfully make an API call to the HackYourFuture GitHub account and display the results inside the DOM. It is not likely that you will need to modify `fetchJSON()` and `createAndAppend()`.
139+
1. Modify **`index.js`**: Modify whatever you need in order to successfully make an API call to the HackYourFuture GitHub account and display the results inside the DOM. It is not likely that you will need to modify `fetchJSON()` and `createAndAppend()`.
140140

141141
2. Add your own CSS styling inside **`style.css`**. Avoid using JavaScript for styling unless there is a genuine need. **You are not allowed to use a CSS library such as Bootstrap.**
142142

Week3/README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ However, in the evolution of programming languages programmers started to think
3232

3333
In this week you'll be exposed to one such programming style that evolved: `object oriented programming` (or OOP for short). OOP is a fundamentally different way of writing software: instead of breaking up a problem in variables and functions that operate on those variables, we break problems up into "entities" that interact with each other.
3434

35-
> Just to make sure you completely get the idea here: OOP is about a different way of thinking about how to write software. The concepts of variables, functions, promises, API (calls) and error handling all still apply. It's just that the way code is organised is differently. Instead of creating long procedures, we create objects that interact with each other.
35+
> Just to make sure you completely get the idea here: OOP is about a different way of thinking about how to write software. The concepts of variables, functions, promises, API (calls) and error handling all still apply. It's just that the way code is organised is different. Instead of creating long procedures, we create objects that interact with each other.
3636
3737
For further study, check the following:
3838

@@ -178,7 +178,7 @@ const noer = new Person('Noer', 27);
178178

179179
### Classes
180180

181-
As you've learned in the previous section, in JavaScript we can do this using `factory/constructor function`.
181+
As you've learned in the previous section, in JavaScript we can do this using `factory or constructor functions`.
182182

183183
Since ES6 we can make use of the `class` keyword, which is a way to create constructor objects as well. It's essentially the same thing as a constructor function, only written in a clearer and more straightforward way.
184184

@@ -198,27 +198,55 @@ const someFunc(param1, callback) {
198198
}
199199
```
200200

201-
Then we learned about how Promises are an improved on callbacks, by providing the developer with a more readable syntax that avoids **callback hell**. We can call them callbacks version 2.0.
201+
Then we learned about how Promises are an improvement upon callbacks, by providing the developer with a more readable syntax that avoids **callback hell**. We can call them callbacks version 2.0. Here's the basic structure again:
202202

203203
```js
204204
new Promise(reject, resolve).then(...);
205205
```
206206

207-
And now we've arrived at the latest upgrade of the callback mechanism: `async/await`.
207+
And now we've arrived at the latest upgrade of the callback mechanism: `async/await`. This construct is part of **ECMAScript 6** and its main benefit is to make using callbacks even more readable. Here's how it might look in action:
208208

209209
```js
210+
async fetchData () {
211+
const fetchedData = await fetch('https://randomuser.me/api/');
212+
const parsedData = await fetchedData.json();
213+
return parsedData;
214+
}
210215
```
211216

217+
This new construct makes use of the Promise object, in the same way that Promises make use of callback functions.
218+
219+
How do we use it? We put the keyword `async` in front of the function declaration that will contain asynchronous code. Then in every line that returns the Promise we put the keyword `await` in front.
220+
221+
For more research, check the following resources:
222+
212223
- [The Evolution of Callbacks, Promises & Async/Await](https://www.youtube.com/watch?v=gB-OmN1egV8)
213224
- [Async JS Crash Course - Callbacks, Promises, Async/Await](https://www.youtube.com/watch?v=PoRJizFvM7s)
214225

215226
### Error handling
216227

217228
As you might have noticed, the Async/Await doesn't give us a way to do error handling like it does in the Promise object.
218229

219-
In the Promise object we have access to the `catch()` function,
230+
In the Promise object we are given access to the `catch()` function, a function whose sole job it is to "catch errors". "Catching errors" is a phrase developers use to indicate various things:
231+
232+
1. that a line of code has caused an error
233+
2. that the program has shutdown to prevent any other errors from happening
234+
3. that the application gives feedback to the developer and/or user
235+
236+
In the Promise object, we can use the function `catch` to take care of errors. It takes in a callback, which automatically receives an error object. Here's an example:
237+
238+
```js
239+
Promise.catch(function(error) {
240+
console.log(error);
241+
});
242+
```
243+
244+
With the Async/Await construction, we don't get that. So instead we have to use some other solution: the `try... catch` block. It's also an addition to the language, given to us by **ECMAScript 6**.
245+
246+
Learn more about it here:
220247

221248
- [JavaScript Try Catch & Error Handling ES6 Tutorial](https://www.youtube.com/watch?v=ye-aIwGJKNg)
249+
- [Error handling, "try..catch"](https://javascript.info/try-catch)
222250

223251
## Finished?
224252

0 commit comments

Comments
 (0)