1 of 13

AJAX, Fetch, and Promises

CSCI 344: Advanced Web Technologies

Spring 2025

2 of 13

Outline

  1. Intro to the JavaScript fetch API
  2. Promises
  3. Summary

3 of 13

Intro to “AJAX”

AJAX

  • AJAX: Stands for “Asynchronous JavaScript and XML”
  • Enables JavaScript to make server requests and (optionally) update the current screen
  • Not easy to tell that information is even being transmitted to/from a server
  • Came on the scene ~2004 (made popular w/Google Mail and Google Maps)

3

4 of 13

Intro to Fetch

JavaScript’s Fetch API

  • The Fetch API is a newer API for browsers to communicate asynchronously with servers communication (improving upon AJAX – the older way)
  • Because this API allows for asynchronous, non-blocking communication with servers, promises were introduced to the JavaScript Language.
  • Promises are objects that help programmers manage asynchronous communications.

4

5 of 13

What is a Promise?

  1. Promises exist to handle asynchronous functionality
  2. Why? If you request a resource the cloud you don’t know how long it’s going to take to process the request.
    1. Slow machine?
    2. Network traffic?
    3. Patchy WIFI!
  3. To handle this, a “promise” allows you to execute a piece of functionality only after the request completes.
    • It’s like putting a process on the “back burner” until it completes…and in the meantime your browser is free to do other things.

6 of 13

Most requests happen in 2 Parts (w/2 Promises)

async function getData () {

const response = await fetch(url);

const data = await response.json();

}

  • fetch() returns a promise containing the response (a Response object). This is just an HTTP response, not the actual content.
  • To extract the body content from the response, we use one of the response’s content methods – in this case the json() method – which also returns a promise with the actual data.

7 of 13

Promises: Two Syntaxes > “then” method

Using the promise’s “then” method, which will execute a callback function when the promise finishes its business...�

let url = 'https://www.apitutor.org/yelp/simple/v3/businesses/search?location=Asheville,+NC'

fetch(url)

.then(response => response.json()) // callback function

.then(data => console.log(data)); // callback function

8 of 13

2. Promises: Two Syntaxes > async await

Using the async / await technique. Instead of using “then()” the await keyword ensures that the next line doesn’t execute until the promise resolves.

const getPosts = async () => {

let url = 'https://www.apitutor.org/yelp/simple/v3/businesses/search?location=Asheville,+NC';

const response = await fetch(url);

console.log(response);

const jsonData = await response.json();

console.log(jsonData);

};

getPosts();

9 of 13

Sample files

Let’s take a look at the Lecture 15 sample files to process these ideas…

10 of 13

Outline

  1. Intro to Web Services and Web APIs
  2. Intro to HTTP
  3. Interacting with a REST APIs
  4. Intro to the JavaScript fetch API
  5. Summary

11 of 13

Summary (1 of 2)

  1. Client-Side Programming Review: We did a quick recap of what we’ve done so far.
  2. Web services / REST APIs allow an organization to make their data, storage, and computing services available over the web via a web server.
    1. You interact with them via HTTP requests that specify actions via methods (GET=read, POST=create, PATCH=update, PUT=replace, DELETE=delete, etc.)
    2. You often need to authenticate with them by including an API key in the header of the HTTP request.
    3. Every web service is different – you need to read the provider’s documentation to understand how to use their service.
    4. Lots of different clients can make HTTP requests (e.g., web browser, most programming languages, PostMan, the command line, etc.)

12 of 13

Summary (2 of 2)

  1. You can interacting with a Web service in most modern programming languages
  2. In JavaScript, you can interact with a server by using the browser’s fetch API and promises.
    1. The fetch API allows you to issue HTTP requests
    2. Promises allow you to be able to handle asynchronous requests
    3. The async / await keywords allow you to create functions that can pause code execution until the promise resolves.

13 of 13

When we come back from break

Read the Homework 3 Instructions �before coming to class