axios.get("https://swapi.dev/api/people/1")
.then(res => {
console.log("RESPONSE : ", res)
})
입력시 출력으로 res의 데이터가 나온다.
fetch("https://swapi.dev/api/people/1") // Promise 반환
.then(res => {
console.log('RESOLVE!', res)
})
axios를 사용하지 않았다면 코드 실행이 너무 빨라 res 데이터가 나오지 않았다.
const getStarWarsPerson = async (id) => {
try {
const res = await axios.get(`https://swapi.dev/api/people/${id}/`);
console.log(res.data);
} catch (e) {
console.log("ERROR", e);
}
}
getStarWarsPerson(5)
비동기식과 id를 사용한 리팩토링
'The Web Developer 부트캠프 2022' 카테고리의 다른 글
API AXIOS 이용한 TV 프로그램 (0) | 2022.09.26 |
---|---|
API Header (0) | 2022.09.26 |
Fetch API (2) | 2022.09.26 |
JSON parese, stringify, API 요청 종류, Http statue (0) | 2022.09.26 |
JS Try, catch 를 이용한 async 문제 해결 (0) | 2022.09.25 |