API 호출시 axios와 fetch의 차이

    fetch API는 보통 이렇게 GET 요청을 하는데 

    fetch(url)
      .then((response) => response.json())
      .then(console.log);

    axios 라이브러리는 이렇게 GET 요청을 할 수 있다.

     

    axios.get('/user?ID=12345')
      .then(function (response) {
        // handle success
        console.log(response);
      })
      .catch(function (error) {
        // handle error
        console.log(error);
      })
      .finally(function () {
        // always executed
      });

    axios는자동으로 JSON 데이터를 Array나 Object로 변환해주는 반면 fetch API 는 .json()으로 변환한다

     

    또한 fetch가 axios보다 조금 빠르다고 한다.

    댓글