Difference between revisions of "Short Notes on JS"

From PaskvilWiki
Jump to: navigation, search
(Created page with "== Fetch Patterns == <pre>fetch("url", { options... }) .then((response) => { if (!response.ok) throw new Error('Network response was not ok'); return response...")
(No difference)

Revision as of 13:21, 24 January 2020

Fetch Patterns

fetch("url", { options... })
  .then((response) => {
    if (!response.ok)
      throw new Error('Network response was not ok');
    return response.json();  // or response.blob(), etc.
  })
  .then((data) => {
    // do something with the data received
  })
  .catch((error) => {
    console.error('Failed to fetch:', error);
  });