Back to all resources
Fetch API
Promises
JSON
Error Handling

Fetch - Get

By Jared Malan
This is a simple JavaScript code snippet that uses the Fetch API to retrieve data from a specified URL, in this case, 'https://api.example.com/data'. The Fetch API provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the network. 

The fetch() method returns a Promise that resolves to the Response object representing the response to the request. This response is then processed and converted into JSON format using the json() method. 

The data received in JSON format is then logged to the console using console.log(). If there is an error at any point during the fetching or processing of the data, it is caught and logged to the console using console.error().