ЁЯУЪ

Complete JavaScript Series: Final Chapter - Fetch API

Jun 6, 2024

Complete JavaScript Series: Final Chapter - Fetch API

Introduction

  • Welcome to the final chapter of the Complete JavaScript Series.
  • Focus: Understanding how to make API calls using JavaScript's Fetch API.
  • We'll create a mini-project: a currency converter.
  • Apply knowledge from the entire series (DOM manipulation, strings, arrays, etc).

Basics of Fetch API

  • Fetch API allows calling API endpoints to receive data.
  • Simple and easier to use compared to promises and callbacks.
  • API (Application Programming Interface): Makes requests and gets responses with data.
  • Example: Requesting a pizza recipe from an API returns data on how to make the pizza.

API Concepts

  • Request-Response Cycle: Send a request, receive a response.
  • Endpoints: URLs to which requests are made.
  • Free Public APIs: Available for various topics (books, business, calendar, etc).
  • Documentation: Essential for understanding how to use different APIs.

Fetch API Usage

  • Syntax: fetch(URL) sends a request to the specified URL.
  • Returns a Promise that resolves to a Response object.
  • Use async/await for handling asynchronous operations.
  • Example: Fetch API that returns cat facts.

JSON and AJAX

  • JSON (JavaScript Object Notation): Lightweight data interchange format, easier than XML.
  • AJAX (Asynchronous JavaScript and XML): Transmitting data asynchronously without refreshing the page.
  • fetch(): Method used for getting resources or data.

Fetch API Practical Example

  • Example API: Fetching cat facts using a public API and displaying them in HTML.
  • DOM Manipulation: Using document.querySelector to interact with HTML elements.
  • Async/Await: Simplifies handling of Promises.
  • Response Methods: response.json() to parse response data.

HTTP Methods

  • GET: Retrieve data from a server (most common use-case with Fetch API).
  • POST: Send data to a server.
  • DELETE, PUT: For deleting and updating data.
  • Status Codes: 200 OK, 404 Not Found, 500 Internal Server Error, etc.

Mini-Project: Currency Converter

  • HTML Structure: Basic layout with input fields, dropdowns, and result display.
  • CSS Styling: Centering content, adding margins, and setting font sizes.
  • JavaScript Functionality: Using Fetch API to get exchange rates and update the DOM.
  • Dynamic Elements: Flags changing based on selected currencies; conversion logic.

Steps to Build Currency Converter

  1. HTML Setup: Create input fields for amount, from and to currencies dropdowns.
  2. CSS Styling: Style the form, dropdowns, and buttons.
  3. Fetch API: Use Fetch API to get exchange rates from an API.
  4. DOM Manipulation: Update flags and conversion results dynamically.
  5. Event Listeners: Handle form submissions and dropdown changes.

Final Thoughts

  • Series summary: Covered major front-end JavaScript concepts.
  • Next steps: Explore backend with Node.js, React.js, or other JavaScript frameworks.
  • Encourage continuous learning and application of knowledge.