JS-151-API
  • JavaScript Library
  • 02-DOM-Manipulation
    • Overview
    • DOM Explained
    • Example Setup
    • Selecting Elements
    • Events
    • Complete Example
  • 03-API Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-Intro-To-APIs
      • Intro
      • Client
      • Requests
      • JSON
      • API Endpoints
      • Server
      • Response
      • Statelessness
      • REST
    • 2-Asynchronous-Programming
      • Intro
      • Callbacks
      • Promises
      • Promises Continued
      • Chaining Promises
    • 3-Fetch
      • Fetch Intro
      • Star Wars
        • Star Wars API
        • Star Wars Setup
        • Star Wars JS
      • Random Photo
        • Unsplash
        • Unsplash Setup
        • Unsplash JS
    • 4-Apps
      • 01-New York Times
        • 00-App Intro
        • 01-HTML/CSS/API Key
        • 02-Variables
        • 03-Event Listeners
        • 04-fetchResults
          • 01-fetchResults()
          • 02-preventDefault()
          • 03-fetch() method
          • 04-Dates
        • 05-displayResults
          • 01-Logging JSON
          • 02-Working with JSON
          • 03-Link Display
          • 04-Results Navigation
          • 05-Results Update
          • 06-Keywords
          • 07-Images
        • 06-Pagination
          • 01-Pagination Intro
          • 02-Pagination Functions
        • 07-Next Steps
      • 02-YouTube
        • html
        • youtube.css
        • youtube.js
      • 03-GoogleMaps
        • Setup
        • HTML and CSS files
        • API Key
        • JS Setup
        • Adding Your Location
        • Listeners
        • Custom Options
Powered by GitBook
On this page
  • FETCH
  • fetch()
  • Say That Again?
  1. 03-API Fundamentals
  2. 4-Apps
  3. 01-New York Times
  4. 04-fetchResults

03-fetch() method

FETCH

In this module, we'll add the fetch method for our GET request and take a look at the results.

fetch()

Just like we've practiced in other small applications, let's use fetch to start talking to the NYT API. Now that the url has been created, we can send a request to the API using the information specified. Add the following to the bottom of the fetchResults method, right before the ending curly bracket:

function fetchResults(e) {
  e.preventDefault();

  url = baseURL + '?api-key=' + key + '&page=' + pageNumber + '&q=' + searchTerm.value;

  //1
  fetch(url)
    .then(function(result) {
    console.log(result)
    return result.json(); //2
  }).then(function(json) {
      console.log(json); //3
  });
}

Let's analyze a little: 1. Remember that fetch is a reserved keyword in JavaScript that allows us to make a request for information, similar to using a GET request with HTTP. The url is given to fetch as a parameter, which sends the request to the url. 2. Meanwhile, it creates a promise containing a result object. This is our response. Remember that we use promises when we have asynchronous, long-running operations. The fetch gets the network resource, which might take a long time to resolve. It will convert the response into a json object by returning the result.json function. 3. That json object is used in another promise (set off by the second .then) to send the information received to another function. For now, we'll use console.log(json) to see the json data.

Say That Again?

Let's go through that again: 1. We make the fetch request. 2. We pass in the NYT url. 3. We create a promise .then that returns a response object called result. 4. The promise asynchronously returns a function that converts the result into usable json format - result.json() is that function call. 5. We create a second promise that has a function that takes in the json object. 6. We log the json object for now.

Previous02-preventDefault()Next04-Dates

Last updated 7 years ago