title |
---|
Week 11 | Web APIs 2 | Asynchronous Programming - Promises - JSON - Fetch API |
{% assign week_num = 11 | to_integer %}
{% if week_num > 0 %}
{% assign previous_week_num = 11 | to_integer | minus: 1 | prepend: '00' | slice: -2, 2 %}
<a href="../week{{ previous_week_num }}">Week {{ previous_week_num }} ⇦</a>
{% endif %}
{% if week_num > 0 %}
{% assign previous_week_num = 11 | to_integer | minus: 1 | prepend: '00' | slice: -2, 2 %}
<a href="../week{{ previous_week_num }}">Week {{ previous_week_num }} ⇦</a>
{% endif %}
Updated: 30/12/2024
How to become a better programmer? "Fail as fast as you can and as often as you can." ~ Scott Hanselman{:target="_blank"}
- Read Introduction to JSON{:target="_blank"} to learn about the world's most popular data format. JSON, which stands for
JavaScript Object Notation
, is a plain text, lightweight data format that can be used across different systems and programming languages.
π You are extraordinary. You know a light-weight data format which you may use to store data or to send it an HTTP server. Now do some exercises for your brain and for your muscle.
JSON Basics
Copy the folder curriculum/week11/exercises/json_basics/
inside folder user/week11/exercises/day01/
and compete all the challenges found inside the JavaScript file.
IMPORTANT: Make sure to complete all the tasks found in the daily Progress Sheet and update the sheet accordingly. Once you've updated the sheet, don't forget to commit
and push
. The progress draft sheet for this day is: /user/week11/progress/progress.draft.w11.d01.csv
You should NEVER update the draft
sheets directly, but rather work on a copy of them according to the instructions found here.
(Nothing here yet. Feel free to contribute if you've found some useful resources.)
Content is based on the following sources:
- 30 Days Of JavaScript: JSON{:target="_blank"} (Permalink){:target="_blank"}
Don't forget to star this awesome repo{:target="_blank"}!
Promises, Promises!{:target="_blank"}
- Read: Promises and Callbacks{:target="_blank"}
Promises are a tough subject in JavaScript, and a lot of developers, even very experienced ones, have issues with them. So you do not have to worry is this concept feel unclear to you.
Below you can find 2 great videos that explain both the Asynchronous topic and Promises
- Watch: Async JavaScript & Callback Functions{:target="_blank"}
- Level: Beginner
- Duration: 24mins
- Captions: Yes
- Watch: JavaScript Promises{:target="_blank"}
- Level: Beginner
- Duration: 37mins
- Captions: Yes
Try to complete as many exercises as you can from the 30 Days Of JavaScript: Promises{:target="_blank"} repository, and don't forget to star the original repo{:target="_blank"} if you enjoyed them and found them helpful.
(Nothing here yet. Feel free to contribute if you've found some useful resources.)
Content is based on the following sources:
- 30 Days Of JavaScript: Promises{:target="_blank"} (Permalink){:target="_blank"}
Don't forget to star this awesome repo{:target="_blank"}!
-
Watch: JavaScript Fetch API π¨π»βπ» in 1 Minute{:target="_blank"}
- Level: Beginner
- Duration: 1min
- Captions: Yes
-
Read: Fetch API{:target="_blank"}
-
Watch Fetch API{:target="_blank"}
- Level: Beginner
- Duration: 30min
- Caption: Yes
Fetch Basics
Copy the folder curriculum/week11/exercises/fetch_basics/
inside folder user/week11/exercises/day03/
and compete all the challenges found inside the JavaScript file.
IMPORTANT: Make sure to complete all the tasks found in the daily Progress Sheet and update the sheet accordingly. Once you've updated the sheet, don't forget to commit
and push
. The progress draft sheet for this day is: /user/week11/progress/progress.draft.w11.d03.csv
You should NEVER update the draft
sheets directly, but rather work on a copy of them according to the instructions found here.
(Nothing here yet. Feel free to contribute if you've found some useful resources.)
Content is based on the following sources:
- 30 Days Of JavaScript: Promises{:target="_blank"} (Permalink){:target="_blank"}
Don't forget to star this awesome repo{:target="_blank"}!
Let's learn about async/await
!
-
Watch: JavaScript Async Await π¨π»βπ» Tutorial in 1 Minute{:target="_blank"}
- Level: Beginner
- Duration: 1min
- Captions: Yes
-
Read: Introduction to Async/Await{:target="_blank"}
-
Watch: Async Await vs. Promises{:target="_blank"}
- Level: Beginner
- Duration: 24mins
- Captions: Yes
(Nothing here yet. Feel free to contribute if you've found some useful resources.)
Content is based on the following sources:
- 30 Days Of JavaScript: Promises{:target="_blank"} (Permalink){:target="_blank"}
Don't forget to star this awesome repo{:target="_blank"}!
Welcome to Asynchronous programming!
Programming Training Wheels: Here are two suggestions that can help you work better with Promises and asynchronous functions:
Suggestion #1:
Remember to handle errors first:
When working with a Promise-based function, using either the await
or .then()
syntax, always start with the error handling structure before moving on to do something with the rest of the code. In the case of the then()
syntax, always start by typing in the catch()
handler, then move on to type the then()
handler:
Step 1
fetch( URL )
.catch( error => console.log(error) ); // <= ALWAYS start this way
Step 2
fetch( URL )
.then( response => /* Now we can start writing the code inside the then() */ )
.catch( error => console.log(error) );
In the case of the await
syntax, you simple start by enclosing the code inside a try/catch
:
try {
const response = await fetch( URL );
} catch( error ){ //<= ALWAYS start this way
console.log( error );
}
Don't stop at console.log:
The second important rule to remember, is not to rely on a simple console.log
for the error cases. Make sure to handle them appropriately. For example, always send some informative response back to the user.
fetch( URL )
.then( data => outputEl.innerHTML = `Stock price: ${data}` );
.catch( error => outputEl.innerHTML = `Ops! Something went wrong: ${error.message}` );
Suggestion #2:
Naming helps: Name all your async Promise-based functions using the Async
suffix:
async function getDataFromFacebookAsync(){
// ...
}
function collectUserDataAsync(){
return new Promise(/* callback function here... */);
}
Having the Async
suffix in your async function will help you remember to handle these functions using await
or the .then().catch()
syntax. Once you get familiar with asynchronous and Promise-based functions, you can get rid of this training wheels even though they will hurt nobody and can probably be of help to some beginner coders that will work on your code.
This will help you avoid common beginners' errors such as trying to get the result of an async function without await
or then()
:
β Wrong:
const response = getDataFromFacebook( URL );
β Correct:
const response = await getDataFromFacebookAsync( URL );
What's the purpose of this?
Just like training wheels on a bicycle, programming training wheels
act as our support and reminders in our first rides with JavaScript. Their role is to instill some core concepts, avoid bugs and common beginner mistakes and also get us accustomed with some of the good practices.
"The functionality of training wheels is based on the premise that a learner rider can gradually develop their balance and coordination skills by relying on the support of the extra wheels. As the rider gains confidence and proficiency, the training wheels are gradually raised or removed, theoretically allowing the rider to transition to riding without additional support." ~ Wikipedia
Now, let's practice what we've learned so far with a few exercises:
-
Async/Await exercise: WaitForResult{:target="_blank"}
-
Promise Refactoring{:target="_blank"}
-
Wikipedia JSON API{:target="_blank"}
-
Bitcoin Wallet{:target="_blank"}
(Nothing here yet. Feel free to contribute if you've found some useful resources.)
Weekly feedback: Hey, it's really important for us to know how your experience with the course has been so far, so don't forget to fill in and submit your mandatory feedback form{:target="_blank"} before the day ends. Thanks you!
<script src="https://utteranc.es/client.js" repo="in-tech-gration/WDX-180" issue-term="pathname" theme="github-dark" crossorigin="anonymous" async> </script>