Skip to main content Accessibility Feedback
  • fetch()
  • Last updated on

fetch()

The Fetch API is used to make Promise-based, asynchronous HTTP requests.

Pass the URL for your HTTP request into the fetch() method as an argument.

In a then() callback method, if the response.ok property is true, return the response.json(). Otherwise, return a rejected Promise. In the next then() callback, you can work with the JSON response data. Use the catch() method to handle errors.

fetch('https://jsonplaceholder.typicode.com/posts').then(function (response) {

	// The API call was successful!
	if (response.ok) {
		return response.json();
	}

	// There was an error
	return Promise.reject(response);

}).then(function (data) {
	// This is the JSON from our response
	console.log(data);
}).catch(function (err) {
	// There was an error
	console.warn('Something went wrong.', err);
});

Source Code