WEB/JavaScript

[JavaScript][비동기] 2. 실습

bay07 2024. 4. 23. 17:36

 

# 02-axios.html 

더보기
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <script>
    const URL = 'https://api.thecatapi.com/v1/images/search'

    // 1.
    <!-- 연습하기 위한 코드 -->
    const promiseObj = axios({
      method: 'get',
      url: URL
    })
    
    // console.log(promiseObj)

    promiseObj.then((response) => {
      console.log(response)
      console.log(response.data)
      console.log(response.data[0].url)
    })

    // cat api로 보낸 요청에 대한 응답을 기다리지않고 넘어옴
    console.log('hahahahahahahahaha')

    // 2.
    <!-- 실제로는 이렇게 쓴다  -->
    axios({
      method: 'get',
      url: URL
    })
      .then((response) => {
        console.log(response)
        console.log(response.data)
        console.log(response.data[0].url)
      })
  </script>

</body>

</html>

# 결과