<AXIOS>
- Promise based HTTP client for the browser and node.js
- Features
- Make XMLHttpRequests from the browser
- Make http requests from node.js
- Supports the Promise API
- Intercept request and response
- Transform request and response data
- Cancel requests
- Automatic transforms for JSON data
- Client side support for protecting against XSRF
https://github.com/axios/axios
-> We don't have to do JSON parsing!
-> 'Catch Error' is worked!
-> Need to add Axios script in Html
axios
.get('http://swapi.co/api/planets/')
.then((res) => {
console.log(res.data);
})
.catch((err) => {
console.log(err);
});
- Chained Request
axios
.get('http://swapi.co/api/planets/')
.then(({ data }) => {
console.log(data);
for (let planet of data.results) {
console.log(planet.name);
}
return axios.get(data.next);
})
.then(({ data }) => {
console.log(data);
for (let planet of data.results) {
console.log(planet.name);
}
})
.catch((err) => {
console.log('ERROR!!!', err);
});
- Refactoring
const fetchNextPlanets = (url = 'http://swapi.co/api/planets/') => {
return axios.get(url);
};
printPlanets = ({ data }) => {
console.log(data);
for (let planet of data.results) {
console.log(planet.name);
}
return Promise.resolve(data.next);
};
fetchNextPlanets()
.then(printPlanets)
.then(fetchNextPlanets)
.then(printPlanets)
.then(fetchNextPlanets)
.then(printPlanets)
.catch((err) => {
console.log('ERROR!!!', err);
});
'javascript' 카테고리의 다른 글
JavaScript/ Parallel vs Sequential & Promise.all (0) | 2020.04.15 |
---|---|
JavaScript/ The Async & The Await (0) | 2020.04.15 |
JavaScript/ Fetch API (0) | 2020.04.14 |
JavaScript/ AJAX & XML Http Request (0) | 2020.04.14 |
JavaScript/ CallBack Hell & Promise! (0) | 2020.04.14 |