API Call in JavaScript – Fetch and Promise

Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses. It also provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the network.

This kind of functionality was previously achieved using XMLHttpRequest.

Live Example

fetch('https://api.coindesk.com/v1/bpi/currentprice.json')
.then((data) => {
  return data.json();
}).then((json) => {
  console.log(json.bpi);
})

Leave a Reply

Your email address will not be published. Required fields are marked *