|
| 1 | ++++ |
| 2 | +title = '🐕 Fetching data' |
| 3 | +headless = true |
| 4 | +time = 20 |
| 5 | +facilitation = false |
| 6 | +emoji= '🧩' |
| 7 | +[objectives] |
| 8 | + 1='Define a client side Web API' |
| 9 | + 2='Define a server side API' |
| 10 | ++++ |
| 11 | + |
| 12 | +So far we have displayed film data stored in our JavaScript code. But real applications fetch data from servers over the internet. We can restate our problem as follows: |
| 13 | + |
| 14 | +> _Given_ an **API that serves** film data |
| 15 | +> _When_ the page first loads |
| 16 | +> _Then_ the page should `fetch` and display the list of film data, including the film title, times and film certificate |
| 17 | +
|
| 18 | +### 💻 Client side and 🌐 Server side APIs |
| 19 | + |
| 20 | +We will use [`fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch), a {{<tooltip title="client side Web API">}} |
| 21 | +A client side [Web API](https://developer.mozilla.org/en-US/docs/Web/API) lives in the browser. They provide programmatic access _to_ built-in browser functions _from_ JavaScript. {{</tooltip>}}. Fetch will fetch our data from the {{<tooltip title="server side API">}} |
| 22 | +A server side API lives on a server. They provide programmatic access _to_ data or functions stored on the server _from_ JavaScript. {{</tooltip>}}. |
| 23 | + |
| 24 | +APIs are useful because they let us get information which we don't ourselves know. The information may change over time, and we don't need to update our application. When we ask for the information, the API will tell us the latest version. |
| 25 | + |
| 26 | +We also don't need to know how the API works in order to use it. It may be written in a different programming language. It may talk to other APIs we don't know about. All we need to know is how to talk to it. This is called the **interface**. |
| 27 | + |
| 28 | +_Using_ fetch is simple. But we want to understand what is happening more completely. So let's take ourselves on a journey through time. |
| 29 | + |
| 30 | +<details> |
| 31 | +<summary>👉🏾 Unfurl to see the journey (we will explain this in little pieces)</summary> |
| 32 | + |
| 33 | +```mermaid |
| 34 | +graph TD |
| 35 | + fetch[(🐕 fetch)] --> |sends a| Request{📤 Request} |
| 36 | + Request --> |has a latency| TimeProblem[🗓️ Time Problem] |
| 37 | + Request --> |to| ServerAPIs |
| 38 | + fetch --> |is a| ClientAPIs |
| 39 | +
|
| 40 | + TimeProblem --> |caused by| SingleThread[🧵 Single thread] |
| 41 | + Callbacks{{🪃 Callbacks}} --> |run on| SingleThread |
| 42 | + SingleThread --> |handled by| EventLoop[🔁 Event Loop] |
| 43 | + EventLoop --> |queues| Callbacks |
| 44 | + SingleThread --> |send tasks to| ClientAPIs |
| 45 | + SingleThread --> |handled by| Asynchrony |
| 46 | +
|
| 47 | + TimeProblem --> |solved by| Asynchrony[🛎️ Asynchrony] |
| 48 | + Asynchrony --> |delivered with| Promise{{🤝 Promises}} |
| 49 | + Asynchrony --> | delivered with | ClientAPIs |
| 50 | + Promise --> |resolve to a| Response{📤 Response} |
| 51 | + Promise --> |join the| EventLoop{{Event Loop 🔁}} |
| 52 | + Promise --> |syntax| async{{🏃♂️ async}} |
| 53 | + async --> |syntax| await{{📭 await}} |
| 54 | + await --> |resolves to| Response |
| 55 | + Response ---> |sequence with| then{{✔️ then}} |
| 56 | +
|
| 57 | +
|
| 58 | + APIs((🧰 APIs)) --> |live in your browser| ClientAPIs{💻 Client side APIs} |
| 59 | + ClientAPIs --> |like| setTimeout[(⏲️ setTimeout)] |
| 60 | + ClientAPIs --> |like| eventListener[(🦻🏾 eventListener)] |
| 61 | + APIs --> |live on the internet| ServerAPIs{🌐 Server side APIs} |
| 62 | + ServerAPIs --> |serve our| Data[(💾 Data)] |
| 63 | + Data --> |as a| Response |
| 64 | +
|
| 65 | +``` |
| 66 | + |
| 67 | +😵💫 This is a lot to take in. Let's break it down and make sense of it. |
| 68 | + |
| 69 | +</details> |
0 commit comments