browser fetch code example

Example 1: javascript fetch api

fetch('http://example.com/movies.json')
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    console.log(data);
  });

Example 2: fetch api javascript

fetch('https://apiYouWantToFetch.com/players') // returns a promise
  .then(response => response.json()) // converting promise to JSON
  .then(players => console.log(players)) // console.log to view the response

Example 3: .fetch method

fetch('http://example.com/movies.json')
  .then(response => response.json())
  .then(data => console.log(data));

Example 4: .fetch method

fetch('http://example.com/data.json')
  .then(data => data);
  .catch(err => console.log(err));

Tags:

Misc Example