The Web Developer 부트캠프 2022

AXIOS

거위발바닥 2022. 9. 26. 15:50
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를 사용한 리팩토링