Skip to content

Instantly share code, notes, and snippets.

@koraytugay
Created August 22, 2021 01:36
Show Gist options
  • Save koraytugay/e9a72d76c60c72959d1a78b626854107 to your computer and use it in GitHub Desktop.
Save koraytugay/e9a72d76c60c72959d1a78b626854107 to your computer and use it in GitHub Desktop.
axios and fetch example
// axios example
const axios = require('axios').default;
// Make a request for a user with a given ID
let employeePromise = axios.get('https://fakejsonapi.com/fake-api/employee/api/v1/employees');
let onfulfilled = function(response) {
// handle success
console.log(response.data.data[0]);
};
let onrejected = function(error) {
// handle error
console.log(error);
};
let onfinally = function() {
// always executed
};
employeePromise
.then(onfulfilled)
.catch(onrejected)
.then(onfinally);
// fetch example
let responsePromise = fetch('https://fakejsonapi.com/fake-api/employee/api/v1/employees');
responsePromise
.then(response => response.json())
.then(data => console.log(data.data[0]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment