"Synchronous basically means that you can only execute one thing at a time. Asynchronous means that you can execute multiple things at a time and you don't have to finish executing the current thing in order to move on to next one."
"The Internet", "The Web", "Web Apps". All of these terms describe something that is interconnected. If you zoom out a bit, the entire internet is basically billions of computers all sharing information and software! But so far your projects have been all alone on your computer :(
Everything you have learned so far happens on the callstack, everything on the callstack executes synchronously. Synchronous means that each line of code will finish executing before the next one starts. Think of infinite loops, your browser freezes because nothing else can happen while the loop is looping!
What makes web development so cool is the ... web. Being able to build applications that connect computers form across the internet. This also introduces some challenges, it can take some time for computers to talk to each other across the internet. You don't want your apps freezing while you wait to hear back from another computer.
Enter asynchronous programming: writing code that tells your browser to start one task and move on to a new task while you wait for the first to finish. This is possible because of the Event Loop.
- Learning Objectives
- Suggested Study
- Sundays & Projects
- Class Recordings
- Curriculum (external)
- HYF Home (external)
How to study the code in this repo.
You will need NPM and nvm on your computer to study this material
Using a browser with good DevTools will make your life easier: Chromium, FireFox, Edge, Chrome
- Install or update the
study-lenses
package globallynpm install -g study-lenses
(if you do not have it already)npm update -g study-lenses
(if you already have it installed)
- Clone this repository:
git clone git@github.com:HackYourFutureBelgium/asynchronous-programming.git
(SSH) (recommended)git clone https://github.com/HackYourFutureBelgium/asynchronous-programming.git
(HTTPS)gh repo clone HackYourFutureBelgium/asynchronous-programming
(GH CLI)
cd
into the repositorycd asynchronous-programming
- Run the
study
command from your CLIstudy
- The material will open in your default browser, you're good to go!
If you have a windows computer and get this error:
... /study.ps1 cannot be loaded because running scripts ...
follow the instructions in this StackOverflow answer, that should take care of it ; )
This repository has two test scripts:
npm run test:esm -- ./path/to/file.js
: used for testing ES Module files in the/browser
directorynpm run test:cjs -- ./path/to/file.js
: used for testing CommonJS files in the/node
directory
- 🥚 You understand the JavaScript Event Loop, and can demonstrate this by using
setTimeout
andsetInterval
to schedule simple tasks. - 🥚 You can explain why Asynchronous Programming is important for programs that have blocking and non-blocking tasks.
- 🥚 You can explain the basics of the Client/Server model and HTTP requests and can
fetch
data from RESTful APIs. - 🥚 You can convert built-in Node modules from consuming callbacks being promises.
- 🐣 You can write and run JavaScript for Node.js or for the Browser. Both environments run JavaScript but they have some important differences!
- use cases: You can explain some use cases that are unique to each runtime.
- built-in APIs: You can list some of the key APIs available in each environment, and why they are not available in the other.
- module systems: You can explain the difference between CommonJS Modules in Node.js and ES Modules in the Browser. You can write and use programs written with either system.
- 🐣 You can break down an asynchronous problem into smaller tasks and solve it using promises. This includes identify which tasks depend on each other and which are independent:
- dependent tasks: The return value from one task is required to start the next task, these must be completed in a specific order -
.then
- independent tasks: These tasks do not use each other's return values, they can be completed at the same time -
Promise.all
- dependent tasks: The return value from one task is required to start the next task, these must be completed in a specific order -
- 🐣 You can trace, refactor and write code that accesses the file system between these 3 different syntaxes:
- Callbacks
- Promises
async
/await
- 🐣 You can step through a Node.js command line application using the VSCode debugger.
- You can write small programs with a Data Access layer that asynchronously uses data stored in different locations:
- 🐣 Browser: You can write a small web page that
fetch
es data from a RESTful API and renders it into the DOM. - 🐥 Node: You can write a small CLI program that reads user input from
process.argv
and reads/writes the file system.
- 🐣 Browser: You can write a small web page that
References and Practice to help you master this module.
expand/collapse
/browser
- 🥚
/the-event-loop
: Learn what the JavaScript Event Loop is and how to visualize it. There are some tools that can help step through asynchronous code, but they will not work for all the code you write. - 🐣
/isolate
: Explore different ways to write and use asynchronous code in JS fromsetTimeout
/setInterval
, to Promises toasync
/await
- 🐣
/promistaurant
: Practice solving asynchronous problems by working in a restaurant. You will need to figure out how you can complete people's orders when all the tasks take different amounts of time and may or may not depend on each other. - 🐣
/api-calls
: Learn how to fetch data from external APIs by modifying a template API Call function. - 🐣
/fetch-and-render
: Practice fetching data from an API and rendering it asynchronously into a user interface.
- 🥚
/node
- 🥚
/commonjs-modules
: Node.js hasn't always usedimport
/export
, it usedrequire
/module.exports
. Explore some examples to understand how this works. - 🥚
/process-argv
: learn to use process.argv to get user input from the command line. you won't need this to write an API, but it's simple enough and helps to understand how command line tools work. - 🐣
/file-system
: practice using thefs
module with callbacks, promises andasync
/await
.
- 🥚
- Coding Nomad - short clear example
- Java Brains - restaurant analogy
- Loupe (+10)
- In the Loop (+10)
- MDN
- flavicops
- javascript.info/settimeout-setinterval
- https://javascript.info/event-loop
- Use case for using setTimeout(0)
- Beau from FCC (timeouts & intervals)
- References
- Practice
- learn-promises
- promise-practice
- JS 30:
- Whack-a-Mole
- Slide in on Scroll
- Countdown Timer
- JS & CSS Clock
- Webcam Fun
- What is JSON?
- APIs 101
- DevTools, the Network Tab:
- What is RESTful
- JSON Placeholder:live, more docs
- restfulapi.net
- Coding Train
- what is CORS?
- HTTP statuses
- References
- Practice
- learn-fetch
- real-world-fetch
- github-api-crash-course tutorial. (hint: avoid pushing your GitHub auth token!)
- Fetching REST
- JS 30: Type Ahead
- Getting started with Node.js debugging in VS Code
- Burke Holland
- VSCode Channel Intro
- James Q Quick
- CodeSpace, 2 Ways
- 101
- Mosh: Node.js in 1 hour
- Traversy: Node for Absolute Beginners
- Traversy: Node.js Crash Course
- NetNinja: Node Js Crash Course
argsv
- Use command line arguments in Node
- Built-In Modules
- The file system (first 20 minutes)
- references
- assert - Test values in Node.js
- fs - Read & write form the file system
- path - Manipulate file paths
- util.promisify - Convert functions that take callbacks into Promises
The Event Loop!
expand/collapse
- ./browser/the-event-loop
- watch a couple of the videos
- step through the timeout examples in jsv9000.app
- ./browser/isolate
labeled-logger
event-loop
examples
- All Together:
- The Event Loop - restaurant analogy
- ./browser/the-event-loop: timeouts examples
- In Small Groups:
- ./browser/isolate: timeout exercises & sync vs. async
- All Together:
- Callback Hell at the Restaurant
- ./the-event-loop: promises
- In Small Groups:
- ./isolate: promises
independent study
No project this week! This week you should focus on studying:
- The Event Loop.
- Reverse-engineering stopwatch.net with
setTimeout
,setInterval
,clearTimeout
andclearInterval
is a good challenge if you're looking for one. (A single script.js file is ok, no need for full architecture)
- Reverse-engineering stopwatch.net with
- Promises
- ./browser/promistaurant will help you understand how to solve asynchronous problems by thinking about what tasks need to be done in a specific order and which tasks can be done at the same time.
async
/await
- Students: Here you can find recordings of this module from past classes. Enjoy!
- Coaches: When sending your PR's with links please ...
- Indicate which class you were teaching
- Which week it was (if the module is more than 1 week)
- Give your name
- and a helpful description
- week 1:
- Part 1: The Event Loop
- Part 2: Whack-a-Mole
- week 2:
- Part 1:
fetch
& REST - Part 2: Explore Users
- Part 1:
- week 3:
- Part 1:
import
andexport
- Part 2: Explore Pokemon
- Part 1:
- week 1:
- Part 1: Isolate - The Event Loop
- Part 2: Integrate - Event Loop
- Part 3: Recap & Project Intro
- week 2:
- Part 1: Isolate - Fetch & REST
- Part 2: Integrate - Fetch & REST
- week 3:
- Part 1: Isolate -
async
/await
- Part 2: Integrate -
async
/await
- Wednesday Review: Deploying to Heroku
- Part 1: Isolate -
- week 1: Scheduling & The Event Loop.
setTimeout
,setInterval
- week 2: Promises &
fetch
- week 3:
async
/await
&fetch
- week 1 - by Yoshi and Joel:
- week 2 - by Yoshi: