đ Project: Space Dashboard
Applying JavaScript asynchronous execution and the Fetch API.
Introduction
Section titled âIntroductionâUse Case Diagram
Section titled âUse Case Diagramâ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.
Project file structure
Section titled âProject file structureâ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
The HTML structure (index.html)
Section titled âThe HTML structure (index.html)â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.
<!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>HTML explanation
Section titled âHTML explanationâEach of the three buttons has a unique ID so JavaScript events can be attached in code.
#statowill display loading progress messages (e.g., âIn progressâŠâ, âOKâ, âErrorâ).#logwill contain the text downloaded from the text file.
Styling (style.css / CSS)
Section titled âStyling (style.css / CSS)âCreate a separate style.css file to give the page the look of a real onboard terminal using 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;}CSS explanation
Section titled âCSS explanationâfont-family: monospacesimulates command terminal fonts.white-space: pre-wrapon the#logcontainer is essential: it preserves line breaks (\n) from the text file when displayed.
Data files (flight logs)
Section titled âData files (flight logs)âCreate two text files in the same project folder:
JavaScript logic (app.js)
Section titled âJavaScript logic (app.js)âBuild the script logic by dividing it into conceptual sections.
Snippet A: Declaring the asynchronous function
Section titled âSnippet A: Declaring the asynchronous functionâ// Use the 'async' keyword to enable 'await' inside the functionasync function caricaLog(nomeFile) {const elementoStato = document.getElementById('stato');const elementoLog = document.getElementById('log');// Loading code...}Snippet B: The HTTP request with await fetch()
Section titled âSnippet B: The HTTP request with await fetch()â// 1. Start the error-handling blocktry { 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; }}Snippet D: Attaching event listeners to the buttons
Section titled âSnippet D: Attaching event listeners to the buttonsâ// Connect the function to the interface buttonsdocument.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'));Testing the code
Section titled âTesting the codeâFurther development
Section titled âFurther developmentâSummary
Section titled âSummaryâKnowledge check
Section titled âKnowledge checkâ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:
awaitpauses 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.txtas an immediate string. - A DOM element representing the selected button.
- A
Promise, representing the functionâs future completion.Correct: an
asyncfunction 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.txtCorrect: the
btn-log2buttonâs event listener callscaricaLog('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
Responseobject.Correct: after receiving the
Response, the code checks its status and reads the body withresponse.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.okisfalse, and the code throws an error.Correct: an HTTP 404 response does not itself reject the
fetch()Promise, so the code checksresponse.okand 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
textContentof the#logelement.Correct:
elementoLogselects#log, andelementoLog.textContent = testodisplays 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
asyncandawaitonly work on a remote server. - Because
.txtfiles can only be read with Live Server. - Because the browser may block
fetch()requests made using thefile:///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
Your score
0%
0 / 0 correct answers
Answered: 0 / 0 (0%)
STICS © 2026 by Carlo Scocco is licensed under CC BY-NC 4.0