Created
August 22, 2021 01:36
-
-
Save koraytugay/e9a72d76c60c72959d1a78b626854107 to your computer and use it in GitHub Desktop.
axios and fetch example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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