[1886px] openweathermap+axios

김유민·2021년 11월 6일

axios란?

axios는 브라우저, Node.js를 위한 Promis API를 활용하는 비동기 통신 라이브러리이다.

npm install axios

1. axios.get

 axios.get("url")
      .then(res => {
        const persons = res.data;
        this.setState({ persons });
      })

get 메서드에는 단순 데이터를 요청할 경우, 파라미터 데이터를 포함시키는 경우로 2가지 상황이 존재한다.

2. async과 await

async을 함수 앞에 붙이면 해당 함수는 항상 promise를 반환한다. await 키워드를 만나면 promis가 처리될 때까지 기다리고 그 이후에 결과가 반환된다.

handleSubmit = async event => {
  event.preventDefault();
  //
  const response = await API.delete(`users/${this.state.id}`);

  console.log(response);
  console.log(response.data);
};

0개의 댓글