Application programming interface: API

In this post, we’ll talk about API (Application programming interface ).

>What is an API?
>How is it used?
>When and where is it used?

API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Each time you use an app like Facebook, send an instant message or check the weather on your phone, you’re using an API.

Calling an API (Part 1) : The Callback Method

<!DOCTYPE html>
<html>
<head>
  <title>AJAX Example</title>
  <script src="ajax.js"></script>
</head>
<body>
  <div class="container">
    <h1>AJAX Example</h1>
    <p>Open up the console :)</p>
  </div>
</body>
</html>
var ajaxRequest = new XMLHttpRequest();
ajaxRequest.addEventListener('readystatechange', function(r) {
  if(r.target.status === 200) {
    console.log(r.target.response);
  }
});
ajaxRequest.open("GET", "https://api.github.com/users/statelyworld", true);
ajaxRequest.send();

Calling an API (Part 2) : The Promises Method

Promises:

JavaScript objects which can be returned synchronously from an asynchronous function.

Promise states
> Pending 	- incomplete
> Fulfilled 	- complete
> Rejected 	- failed
<!DOCTYPE html>
<html>
<head>
  <title>AJAX Example</title>
  <script src="ajax.js"></script>
</head>
<body>
  <div class="container">
    <h1>AJAX Example</h1>
    <p>Open up the console :)</p>
  </div>
</body>
</html>
fetch('https://api.github.com/users/statelyworld')
  .then(function(r) {
    console.log(r.status);
    return r.json();
  })
  .then(function(j) {
    console.log(j);
  })