๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์˜ˆ์‹œ ์ฝ”๋“œ

1. index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <script src="./main.js" type="text/javascript"></script>
  </head>
  <body>
    <div class="container">
      <h4 class="text-center">๊ฒŒ์‹œํŒ - Main</h4>

      <div class="input-group mb-3">

      <table class="table table-hover">
        <thead>
          <tr>
            <th>#</th>
            <th>์ œ๋ชฉ</th>
            <th>์ž‘์„ฑ์ž</th>
            <th>์ž‘์„ฑ์ผ์‹œ</th>
            <th>์กฐํšŒ์ˆ˜</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>{{ board.boardId }}</td>
            <td>{{ board.title }}</td>
            <td>{{ board.userName }}</td>
            <td>{{ board.regDt.date | makeDateStr(".") }}</td>
            <td>{{ board.readCount }}</td>
          </tr>
        </tbody>
      </table>
      <div style="color: red;" class="btn btn-sm btn-primary">
        <a href='javascript:void(0);' onclick="test();">test</a>
        <a href="javascript:getHello();">๊ธ€์“ฐ๊ธฐ</a>
        <a href="javascript:getHello2();">HelloPOST</a>
      </div>
    </div>
  </body>
</html>

2. main.js

function init() {
  console.log("===init===");
}
init();

async function test() {
  console.log("===test===");
}

function getData(url) {
  return fetch(url).then(function (data) {
    return data.json();
  });
}

async function getHello() {
  const url = "http://localhost:8080/hello";
  const result = await getData(url);
  console.log(result);
  document.write(result.name);
  console.log(result.name);
  return result;
}

// JSON.stringify() : JSON๊ฐ์ฒด๋กœ ์ง๋ ฌํ™” (js๊ฐ์ฒด => JSON)
// JSON.parse() : Javascript ๊ฐ์ฒด๋กœ ์—ญ์ง๋ ฌํ™” (JSON => js๊ฐ์ฒด)
async function getHello2() {
  param = {
    test: "[test param]",
  };
  const url = "http://localhost:8080/hello2";
  fetch(url, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    // body: JSON.stringify({
    //   test: "[test param]",
    // }),
    body: JSON.stringify(param),
  })
    // ์ฒซ ๋ฒˆ์งธ then: ์•„์ง body๋„์ฐฉ ์ „, promise ๊ฐ์ฒด
    // ๋น„๋™๊ธฐ๋กœ ์ฒ˜๋ฆฌ๋œ fetch ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ ๊ฐ’์ด ๋“ค์–ด์˜ค๋ฉด then ๋ฉ”์†Œ๋“œ์˜ ์•ก์…˜์„ ์‹คํ–‰ํ•œ๋‹ค.
    // fetchํ•จ์ˆ˜๋Š” ์„œ๋ฒ„์—์„œ ์ „๋‹ฌ๋ฐ›์€ Promise ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด, ๋ฆฌํ„ด๋œ ๊ฐ์ฒด๋ฅผ then๋ฉ”์†Œ๋“œ์—์„œ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค. => ํ—ค๋”๋Š” ๋„์ฐฉ, ๋ฐ”๋””๋Š” ๋„์ฐฉ ์ „
    // .json()์€ Promise ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
    // Promise ์ž‘๋™๋ฐฉ์‹: fetch ํ›„ ๋‚ ์•„์˜ค๋Š” response๊ฐ์ฒด๋Š” ์ผ๋‹จ header๋จผ์ € ๋„์ฐฉํ•จ. ๋ชจ๋“  header ๋„์ฐฉํ•˜์ž๋งˆ์ž return๋จ
    // ์•„์ง body๊ฐ€ ์•ˆ ์˜จ ์ƒํƒœ์ธ Promise๊ฐ์ฒด๋ฅผ ๋ฐ”๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ž„.
    // ์ค‘๊ฐ„์— ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ body๊นŒ์ง€ ๋‹ค ์˜จ Promise๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ์‹ถ์ง€๋งŒ ๊ทธ๊ฑด ๋ถˆ๊ฐ€๋Šฅํ•จ.
    // ๋•Œ๋ฌธ์— .then์œผ๋กœ chaining(์ฒด์ด๋‹)ํ•ด์„œ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค.
    .then((result) => {
      return result.json();
    })
    .then((result) => {
      console.log(result);
      document.write(result.name);
      console.log(result.name);
    })
    .catch((error) => {
      console.log(error);
      alert("error๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.");
    });
}

 

 

HTTP ๋ฉ”์†Œ๋“œ์— ๋”ฐ๋ฅธ API ํ˜ธ์ถœ

- GET

async function getHello() {
  const url = "http://localhost:8080/hello";
  const result = await getData(url);
  console.log(result.name);
  return result;
}

 

- POST

Promise ๊ฐ์ฒด ์ฒด์ด๋‹X

/**
 * Promise ์ฒด์ด๋‹X
 */
async function getHello3() {
  param = {
    test: "[test param]",
  };
  const url = "http://localhost:8080/hello2";
  const res = await fetch(url, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(param),
  });

  const result = await res.json();

  console.log(result.name);
}

Promise ๊ฐ์ฒด ์ฒด์ด๋‹O

async function getHello2() {
  param = {
    test: "[test param]",
  };
  
  const url = "http://localhost:8080/hello2";
  fetch(url, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(param),
  })
    .then((result) => {
      return result.json(); //์—ฌ๊ธฐ์„œ Promise ๊ฐ์ฒด๊ฐ€ ๋‚˜์˜ด
    })
    .then((result) => {
      console.log(result.name);
    })
    .catch((error) => {
      console.log(error);
      alert("error๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.");
    });
}

 

1. then์„ 2๋ฒˆ ์“ฐ๋Š” ์ด์œ 

- Promise์˜ ์ž‘๋™ ๋ฐฉ์‹ ๋•Œ๋ฌธ์ด๋‹ค.

- .json์„ ํ•ด๋„ json๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ Promise๊ฐ์ฒด๊ฐ€ ๋ฆฌํ„ด๋œ๋‹ค. 

* ๋น„๋™๊ธฐ๋กœ ์ฒ˜๋ฆฌ๋œ fetch()๋Š” ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ›์œผ๋ฉด then๋ฉ”์†Œ๋“œ์˜ ์•ก์…˜์„ ์‹คํ–‰ํ•œ๋‹ค.

* fetch()๋ฅผ ํ•˜๋ฉด ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ response ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ๋ฆฌํ„ด๋œ ๊ฐ์ฒด๋Š” then๋ฉ”์†Œ๋“œ์˜ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค.

- ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ response ๊ฐ์ฒด๋ฅผ ๋ฐ›์„ ๋•Œ, ๋ชจ๋“  header๊ฐ€ ๋„์ฐฉํ•˜๋ฉด ๋ฐ”๋กœ returnํ•œ๋‹ค.

   => ์•„์ง body๊ฐ€ ์˜ค์ง€ ์•Š์€ ์ƒํƒœ, body๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ๋Š” ์ƒํƒœ์ธ Promise ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

- ์ค‘๊ฐ„์— ๊ธฐ๋‹ค๋ ค์„œ body๊นŒ์ง€ ๋„์ฐฉํ•œ Promise ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ์• ์ดˆ์— header๊ฐ€ ๋„์ฐฉํ•˜์ž๋งˆ์ž ๋ฐ”๋กœ ๋ฆฌํ„ดํ•ด๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์—, .then์œผ๋กœ chaining(์ฒด์ด๋‹) ํ•˜์—ฌ body๊ฐ’์„ ๋ฐ›์•„์•ผ ํ•œ๋‹ค.

 

Promise ๊ฐ์ฒด๋ž€?

- ๋น„๋™๊ธฐ ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ์™„๋ฃŒ๋์„ ๋•Œ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ชจ๋“ˆ
- ๋น„๋™๊ธฐ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด
- ์‚ฌ์šฉ์ด์œ  : ๊ธฐ์กด์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜ ์‚ฌ์šฉ ์‹œ ์ฝœ๋ฐฑ์ง€์˜ฅ ๋ฐœ์ƒ, ๊ฐ€๋…์„ฑ ๋–จ์–ด์ง€๊ณ  ์œ ์ง€๋ณด์ˆ˜์„ฑ ๋‚ฎ์•˜์Œ.

 

Promise Chaining (์—ฌ๋Ÿฌ Promise ์—ฐ๊ฒฐ)์ด๋ž€?

- .then์„ ๋‚˜์—ดํ•˜์—ฌ ์ œ์–ดํ•˜๋Š” ๋ฐฉ์‹.
- then์—์„œ returnํ•ด์ฃผ๊ณ  ๋‹ค์Œ then์—์„œ ์ด๋ฅผ ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹.

 

 

2. Javascript ๊ฐ์ฒด์™€ JSON ๊ฐ์ฒด

- JSON.stringify() : Javascript => JSON ๊ฐ์ฒด๋กœ ์ง๋ ฌํ™”

- JSON.parse() : JSON => Javascipt ๊ฐ์ฒด๋กœ ์ง๋ ฌํ™”