😇Chú ý lớn nhất về async, await (ok)

C:\xampp\htdocs\code\express\index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>JSON Test</title>
</head>

<body>
  <div id="myData"></div>
  <script type="text/javascript">
  function fetchMoviesJSON() {
    var abc = fetch('https://jsonplaceholder.typicode.com/todos/?_limit=5')
      .then((response) => {
        return response.json();
      })
      .then((response) => {
        return response;
      })
      .catch(function(err) {
        console.log('Fetch Error :-S', err);
      });
    console.log(abc);
  }
  fetchMoviesJSON();
  </script>
  <p>Chú ý 1: Nếu không sử dụng async cho fetchMoviesJSON thì kết quả trả về: Promise{<pending>}</p>
  <script type="text/javascript">
   async function fetchMoviesJSONA() {
    var abc = await fetch('https://jsonplaceholder.typicode.com/todos/?_limit=5')
      .then((response) => {
        return response.json();
      })
      .then((response) => {
        return response;
      })
      .catch(function(err) {
        console.log('Fetch Error :-S', err);
      });
    console.log(abc);
  }
  fetchMoviesJSONA();
  </script>
  <p>Chú ý 2: Nếu không sử dụng async cho fetchMoviesJSON thì kết quả trả về: [{…}, {…}, {…}, {…}, {…}]}</p>
</body>

</html>

Last updated

Was this helpful?