Fetching data from the server - Learn web development | MDN

PHOTO EMBED

Mon May 23 2022 15:51:41 GMT+0000 (Coordinated Universal Time)

Saved by @zaccamp

// Call `fetch()`, passing in the URL.
fetch(url)
  // fetch() returns a promise. When we have received a response from the server,
  // the promise's `then()` handler is called with the response.
  .then( response => {
    // Our handler throws an error if the request did not succeed.
    if (!response.ok) {
      throw new Error(`HTTP error: ${response.status}`);
    }
    // Otherwise (if the response succeeded), our handler fetches the response
    // as text by calling response.text(), and immediately returns the promise
    // returned by `response.text()`.
    return response.text();
  })
  // When response.text() has succeeded, the `then()` handler is called with
  // the text, and we copy it into the `poemDisplay` box.
  .then( text => poemDisplay.textContent = text )
  // Catch any errors that might happen, and display a message
  // in the `poemDisplay` box.
  .catch( error => poemDisplay.textContent = `Could not fetch verse: ${error}`);
content_copyCOPY

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Fetching_data