Skip to content

🚀 Project: Space Dashboard

Applying JavaScript asynchronous execution and the Fetch API.

flowchart LR User(["đŸš¶ User"]) subgraph System ["Asynchronous space dashboard"] U1(["Select a flight log"]) U2(["View the log contents"]) U3(["View the transmission status"]) U4(["View an error message"]) U1 -.-> U2 U1 -.-> U3 U1 -.-> U4 end User --> U1 classDef actor fill:#1e3a8a,stroke:#3b82f6,color:#fff,stroke-width:2px; classDef usecase fill:#2563eb,stroke:#1d4ed8,color:#fff,stroke-width:2px; classDef system fill:#f8fafc,stroke:#64748b,stroke-width:2px,color:#0f172a; class User actor; class U1,U2,U3,U4 usecase; class System system;

Diagram created with Mermaid

The use case diagram describes the space dashboard flow: the user selects a flight log, the system asynchronously waits for the response, and displays the content in the DOM, the transmission status, or a network or file-not-found error.

To complete this exercise, create a working folder on your computer and add the required files using this structure:

  • Directoryspace-mission/
    • index.html User interface with buttons and a command console
    • style.css Custom terminal-style stylesheets
    • app.js Asynchronous JavaScript logic using the Fetch API
    • log1.txt Day 1 flight log text file
    • log2.txt Day 42 flight log text file

Let’s start by creating the user interface. HTML defines the control buttons and the console area where the download status and log text will appear.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ares-IV Mission - Flight Log</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>🚀 Ares-IV Mission: Flight Log</h1>
<p>Select a log to download from the central server:</p>
<!-- Control buttons -->
<button id="btn-log1">Day 1 Log</button>
<button id="btn-log2">Day 42 Log</button>
<button id="btn-errore">Test Error (404)</button>
<!-- Display console -->
<div class="console">
<strong>Connection status:</strong> <span id="stato">Waiting...</span>
<hr>
<div id="log">No data loaded.</div>
</div>
<script src="app.js"></script>
</body>
</html>

Each of the three buttons has a unique ID so JavaScript events can be attached in code.

  • #stato will display loading progress messages (e.g., “In progress
”, “OK”, “Error”).
  • #log will contain the text downloaded from the text file.

Create a separate style.css file to give the page the look of a real onboard terminal using CSS.

style.css
body {
background-color: #0d1117;
color: #58a6ff;
font-family: monospace;
padding: 20px;
}
.console {
border: 1px solid #30363d;
padding: 15px;
border-radius: 6px;
background-color: #161b22;
margin-top: 15px;
}
button {
background-color: #238636;
color: white;
border: none;
padding: 10px 15px;
margin-right: 8px;
cursor: pointer;
border-radius: 4px;
}
button:hover {
background-color: #2ea043;
}
#log {
margin-top: 15px;
white-space: pre-wrap;
color: #c9d1d9;
}
  • font-family: monospace simulates command terminal fonts.
  • white-space: pre-wrap on the #log container is essential: it preserves line breaks (\n) from the text file when displayed.

Create two text files in the same project folder:

Build the script logic by dividing it into conceptual sections.

// Use the 'async' keyword to enable 'await' inside the function
async function caricaLog(nomeFile) {
const elementoStato = document.getElementById('stato');
const elementoLog = document.getElementById('log');
// Loading code...
}
// 1. Start the error-handling block
try {
elementoStato.textContent = `Downloading ${nomeFile}...`;
// 2. Send the request and WAIT for the response headers
const response = await fetch(nomeFile);
// 3. Check whether the file exists on the server (e.g., HTTP 200 OK)
if (!response.ok) {
throw new Error(`File not found (HTTP status: ${response.status})`);
}
}

Snippet C: Converting the response to text and updating the DOM

Section titled “Snippet C: Converting the response to text and updating the DOM”
// 4. WAIT for the entire file content to be read as text
const testo = await response.text();
// 5. Update the interface with the received data
elementoStato.textContent = 'Connection completed successfully.';
elementoLog.textContent = testo;
} catch (errore) {
// Handle network issues or missing files
elementoStato.textContent = 'TRANSMISSION ERROR!';
elementoLog.textContent = errore.message;
}
}
// Connect the function to the interface buttons
document.getElementById('btn-log1').addEventListener('click', () => caricaLog('log1.txt'));
document.getElementById('btn-log2').addEventListener('click', () => caricaLog('log2.txt'));
document.getElementById('btn-errore').addEventListener('click', () => caricaLog('log-inesistente.txt'));

Your answers to this page's quizzes are saved in your browser's local storage and remain there between visits.

Why does caricaLog() use async and await?

  • To block the dashboard until the download is complete.
  • To wait for the response and read the log without blocking the page.

    Correct: await pauses the function while the page remains responsive.

  • To execute text files as JavaScript code.

What does a function declared with async, such as caricaLog(), return?

  • The contents of log1.txt as an immediate string.
  • A DOM element representing the selected button.
  • A Promise, representing the function’s future completion.

    Correct: an async function always returns a Promise, even when no value is explicitly returned.

Which file is requested when the “Day 42 Log” button is selected?

  • log1.txt
  • log2.txt

    Correct: the btn-log2 button’s event listener calls caricaLog('log2.txt').

  • log-inesistente.txt

What does fetch(nomeFile) return before the dashboard reads the log?

  • The file text directly.
  • A Boolean indicating whether the file exists.
  • A Promise that resolves to a Response object.

    Correct: after receiving the Response, the code checks its status and reads the body with response.text().

What happens if the “Test Error (404)” button receives an HTTP 404 response?

  • fetch() always automatically throws an error for every 404 response.
  • response.ok is false, and the code throws an error.

    Correct: an HTTP 404 response does not itself reject the fetch() Promise, so the code checks response.ok and throws an error.

  • The dashboard displays the contents of log2.txt.

How is successfully downloaded text displayed in the dashboard?

  • It is inserted into the button that was clicked.
  • It is assigned to the textContent of the #log element.

    Correct: elementoLog selects #log, and elementoLog.textContent = testo displays the content as text.

  • It is written to the developer tools console.

Why should the project be run through a local server instead of opening index.html with a double-click?

  • Because async and await only work on a remote server.
  • Because .txt files can only be read with Live Server.
  • Because the browser may block fetch() requests made using the file:/// protocol.

    Correct: a local server serves the page over HTTP and allows it to request the log files.

Quiz progress

0 / 0 questions answered (0%)

0 correct answers