๐ŸฆŠcodecamp 4~6์ผ์ฐจ ์ •๋ฆฌ๋ณธ

JBoBยท2023๋…„ 2์›” 25์ผ
0
post-custom-banner

๐ŸฆŠRest-API ๋งŒ๋“ค๊ธฐ

๋งŒ๋“ค๊ธฐ ์ „ ๋ผ์šฐํŒ… ๊ฐœ๋…์„ ์ตํ˜€๋ณด์ž

๋ผ์šฐํŒ…

: URL ๋ฐ ํŠน์ •ํ•œ HTTP ์š”์ฒญ ๋ฉ”์†Œ๋“œ(GET,POST ๋“ฑ)์ธ ํŠน์ • ์—”๋“œํฌ์ธํŠธ์— ๋Œ€ํ•œ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‘๋‹ตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ

Rest-API์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์š”์ฒญ์€ GET ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

์—”๋“œํฌ์ธํŠธ๋กœ get ์š”์ฒญ์ด ๋“ค์–ด์™”์„๋•Œ, ๋ฐ์ดํ„ฐ๊ฐ€ ์กฐํšŒ ๋ ์ˆ˜ ์žˆ๋„๋ก ์šฐ๋ฆฌ๋Š” ๋กœ์ง์„ ์ถ”๊ฐ€ํ•ด ์ค˜์•ผํ•œ๋‹ค.

๋ณดํ†ต ๋ฐ์ดํ„ฐ๋Š” ํ•˜๋‚˜์˜ ๊ฐ’์ด ์•„๋‹Œ , ์—ฌ๋Ÿฌ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๋ฐฐ์—ด ์•ˆ์— ๊ฐ์ฒด๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํ˜•ํƒœ๊ฐ€ ๋Œ€๋ถ€๋ถ„์ด๋‹ค.

// get๋ฐฉ์‹์˜ API๋ฅผ ๋งŒ๋“ค๊ฒ ๋‹ค
app.get('/boards', function (req, res) {
  // 1.DB์— ์ ‘์†ํ›„, ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ => ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์กฐํšŒํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •.
  const result = [
    { number: 1, writer: "cho", title: "์ œ๋ชฉ์ž…๋‹ˆ๋‹ค", contents: "๋‚ด์šฉ์ด์—์š”!" },
    { number: 2, writer: "jo", title: "๋ฐ”๋ณด์ž…๋‹ˆ๋‹ค", contents: "๋ฐ”๋ณด์—์š”!" },
    { number: 3, writer: "hyun", title: "์ฃผํ˜„์ž…๋‹ˆ๋‹ค", contents: "์ฃผํ˜„์ด์—์š”!" }
  ]

  // 2.DB์—์„œ ๊บผ๋‚ด์˜จ ๊ฒฐ๊ณผ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ต(response) ์ฃผ๊ธฐ 
  
  res.send(result)
})

app.listen(3000, () => {
  console.log("๋ฐฑ์—”๋“œ API ์„œ๋ฒ„๊ฐ€ ์ผœ์กŒ์–ด์š”!!!")
})

๐Ÿ‘ปย req, res๋ž€?

get ํ•จ์ˆ˜์˜ ๋‘๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ๋ฐ›๊ณ  ์žˆ๋Š”๋ฐ req, res ๋ผ๋Š” ๋‘ ๋ณ€์ˆ˜๋ฅผ ๋ฐ›๊ณ  ์žˆ๋‹ค.

req๋Š” ์š”์ฒญ ์ด๊ณ  res๋Š” ์‘๋‹ต์ด๋‹ค.

์ด๋ฒˆ์—๋Š” POST ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋กœ์ง์„ ์งœ๋ณด๊ฒ ๋”ฐ.

// index.js

// ... ๊ธฐ์กด ์ฝ”๋“œ

app.post('/boards', (req, res) => {
  // 1. ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋ณด๋‚ด์ค€ ๋ฐ์ดํ„ฐ ํ™•์ธํ•˜๊ธฐ
===>req.body๋กœ ๋ฐ์ดํ„ฐ ์ €์žฅํ•˜๋Š”๊ฒƒ ํ™•์ธํ•ด์ฃผ๊ธฐ
	console.log(req)
  console.log("=========================")
  console.log(req.body) // ์ถ”๊ฐ€	
	// 2. DB์— ์ ‘์† ํ›„, ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ => ๋ฐ์ดํ„ฐ ์ €์žฅํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •

  // 3. DB์— ์ €์žฅ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ต(response) ์ฃผ๊ธฐ
  res.send("๊ฒŒ์‹œ๋ฌผ ๋“ฑ๋ก์— ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค.")
});

app.listen(3000, () => {
  console.log("๋ฐฑ์—”๋“œ API ์„œ๋ฒ„๊ฐ€ ์ผœ์กŒ์–ด์š”!!!")
})

์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๊ฐ€ ํด๋ผ์ด์–ธํŠธ ์—์„œ ๋ณด๋‚ธ HTTP ์š”์ฒญ ์ •๋ณด๊ฐ€ req์— ์ž‘์„ฑ๋˜์–ด์žˆ๋‹ค๊ณ  ๋ฐฐ์› ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด post์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š”๊ฒƒ์„ ํ™•์ธํ•˜๋ ค๋ฉด req.body ๋ฅผ ์ด์šฉํ•ด์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ req ์™€ req.body๋ฅผ ํ™•์ธ ํ• ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์ž.

ํ•˜์ง€๋งŒ postmen์œผ๋กœ ์š”์ฒญ์œผ๋กœ ๋ณด๋‚ด์ฃผ๊ฒŒ ๋˜๋ฉด ์„ฑ๊ณต์ ์œผ๋กœ ์‘๋‹ตํ•˜์ง€๋งŒ

req.body์—๋Š” undefined ๊ฐ€ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š”??

express ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” jsonํ˜•ํƒœ๋ฅผ ์ง€์›ํ•˜๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์„œ๋ฒ„์—์„œ json์„ ์ฝ์–ด์˜ค๊ธฐ ์œ„ํ•ด app.use(express.json()) ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

// index.js

// const express = require('express')
import express from 'express';

const app = express();

app.use(express.json()); // ์ถ”๊ฐ€๋œ ์ฝ”๋“œ
app.get('/boards', (req, res) => {

// ... ์ƒ๋žต

๐ŸคcreateTokenOfPhone โ†’ Rest-API๋กœ ๋งŒ๋“ค๊ธฐ

app.post("/tokens/phone", function(req, res){
  
  // post body ๋งŒ๋“ค๊ธฐ (๋‚ด๊ฐ€ํ•œ๊ฑฐ)
  // req.body.qqq
  // console.log(req.body);
   //1.๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น
  // const {myphone} = req.body;
   //2.๋‹ค๋ฅธ ๋ฐฉ๋ฒ•
  // const myphone = req.body[myphone]; 

  // post body ๋งŒ๋“ค๊ธฐ 
  const myphone = req.body.qqq

  
  // 1. ํœด๋Œ€ํฐ ๋ฒˆํ˜ธ ์ž๋ฆฟ์ˆ˜ ๋งž๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ (10~11์ž๋ฆฌ)
  const isValid = checkPhone(myphone)
  if(isValid ===  false){
      return
  }

 // 2. ํœด๋Œ€ํฐ ์ธ์ฆ๋ฒˆํ˜ธ 6์ž๋ฆฌ ์ƒ์„ฑ
  const mytoken = getToken()

 // 3. ๋ฒˆํ˜ธ์— ์ธ์ฆ๋ฒˆํ˜ธ(ํ† ํฐ) ์ „์†กํ•˜๊ธฐ
  sendTokenToSMS(myphone, mytoken)

  res.send("์ธ์ฆ์™„๋ฃŒ")
})

๐ŸงNode.js 5์ผ์ฐจ

๐ŸคAxios

  • Axios๋Š” ๋ธŒ๋ผ์šฐ์ €,Node.js๋ฅผ ์œ„ํ•œ Promise API๋ฅผ ํ™œ์šฉํ•˜๋Š” HTTP ๋น„๋™๊ธฐ ํ†ต์‹  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.
  • ๋ฐฑ์—”๋“œ๋ž‘ ํ”„๋ก ํŠธ์—”๋“œ๋ž‘ ํ†ต์‹ ์„ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ Ajax์™€ ๋”๋ถˆ์–ด ์‚ฌ์šฉํ•œ๋‹ค.

ํŠน์ง•

  • ES6 ์‚ฌ์šฉ
  • HTTP ์š”์ฒญ๊ณผ ์‘๋‹ต์„ JSON ํ˜•ํƒœ๋กœ ์ž๋™ ๋ณ€๊ฒฝ
  • ์š”์ฒญ๊ณผ ์‘๋‹ต ๋ฐ์ดํ„ฐ์˜ ๋ณ€ํ˜•

Axios ์„ค์น˜

yarn install axios

================
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
// HTML ์ถ”๊ฐ€ 

Axios ๋ฌธ๋ฒ•

.then : ๋น„๋™๊ธฐ ํ†ต์‹ ์ด ์„ฑ๊ณตํ–ˆ์„ ๊ฒฝ์šฐ, ์ฝœ๋ฐฑ์„ ์ธ์ž๋กœ ๋ฐ›์•„ ๊ฒฐ๊ณผ๊ฐ’์„ ์ฒ˜๋ฆฌ

.catch : ์˜ค๋ฅ˜ ์ดˆ๋ฆฌ

<head>
    <title>ํšŒ์›๊ฐ€์ž… ์—ฐ์Šตํ•˜๊ธฐ</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
      function check() {
        // 1. ์ž…๋ ฅํ•œ ํœด๋Œ€ํฐ๋ฒˆํ˜ธ ๊ฐ€์ ธ์˜ค๊ธฐ
        const myphone = document.getElementById('qqq').value;
        console.log('๋‚˜์˜ ํ•ธ๋“œํฐ๋ฒˆํ˜ธ: ', myphone);

        // 2. ํ•ด๋‹น ํœด๋Œ€ํฐ๋ฒˆํ˜ธ๋กœ ์ธ์ฆ๋ฒˆํ˜ธAPI ์š”์ฒญํ•˜๊ธฐ
        axios
          .post('http://localhost:3000/tokens/phone', {
            qqq: myphone,
          })
          .then((res) => {
            console.log(res);
						document.getElementById("zzz").innerText = res.data
          });
      }
    </script>
  </head>

[axios.post](http://axios.post) ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ API์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.

axios.then ์„ ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ์„ ๋ณด๋‚ธ ํ›„, ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ๋ฉด ๋˜๋Š”๋ฐ ์š”์ฒญ์„ ๋Œ€ํ•œ ์‘๋‹ต์„ ๋ฐ›์•„์ค€๋‹ค.

ํ•˜์ง€๋งŒ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ์‘๋‹ต์ด ์˜จ๊ฒƒ์ธ์ง€ ํ™•์ธํ• ์ˆ˜๊ฐ€ ์—†๋‹ค.

์ž์„ธํžˆ ๋ณด๋ฉด cors๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ๋ˆˆ์— ๋„๋Š”๋ฐ cors๋ž€ ๋ญ˜๊นŒ?

๐Ÿ™CORS๋ž€?

CORS๋ž€, Cross-Origin-Resource-Sharing์˜ ์•ฝ์ž์ด๋‹ค.

์ฆ‰, ์„œ๋กœ ๋‹ค๋ฅธ ์ถœ์ฒ˜(origin)๋ฅผ ๊ฐ€์ง„ ์ฃผ์†Œ๋กœ ์š”์ฒญ์ด ๋“ค์–ด์™”์„๋•Œ ๋ฐœ์ƒํ• ์ˆ˜ ์žˆ๋Š” ์—๋Ÿฌ๋‹ค.

์ถœ์ฒ˜๋Š” port๋ฒˆํ˜ธ๊นŒ์ง€ ํฌํ•จํ•œ url์ด๋‹ค.

์ง€๊ธˆ ์œ„์— ์ ์€ ์ด๋Ÿฌํ•œ url(http://localhost:3000/tokens/phone)์—์„œ http://localhost:3000์„ origin์ด๋ผ ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

cors ๋ฌธ์ œ ํ•ด๊ฒฐํ•˜๊ธฐ

์ง์ ‘ ์‘๋‹ตํ—ค๋”์— ์ถœ์ฒ˜๋ฅผ ์ ์–ด์ฃผ๋Š” ๋ฐฉ์‹๋„ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์กฐ๊ธˆ ๋” ํŽธํ•œ cors๋ฏธ๋“ค์›จ์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด์ฃผ์ž.

cors ์„ค์น˜ํ•˜๊ธฐ

yarn add cors

=============
import cors from 'cors'
app.use(cors())
app.use(express.json())
  • importํ•œ cors๋ฅผ ์ ์šฉ์‹œ์ผœ์ฃผ๊ธฐ ์œ„ํ•˜์—ฌ app.use ๋ฅผ ์‚ฌ์šฉํ•ด์ค€๋‹ค.
    • app.use(cors()) ์„ ์ž…๋ ฅํ•˜๋ฉด ๋ชจ๋“  ์ถœ์ฒ˜์—์„œ ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์„ ํ—ˆ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.
    • ์ถ”ํ›„ ํŠน์ • ๋ณด์•ˆ๊ณผ ๊ด€๋ จํ•ด ํŠน์ • ์ถœ์ฒ˜๋งŒ์„ ํ—ˆ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด, app.use(cors({origin:})

๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜์—ฌ ํŠน์ • origin๋งŒ์„ ํ—ˆ์šฉํ•˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ™apollo-server ์„ค์น˜

์šฐ๋ฆฐ ์ง€๊ธˆ๊นŒ์ง€ Rest-API์— ๋Œ€ํ•ด ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด express

๋ฅผ ์‚ฌ์šฉํ•ด ์„œ๋ฒ„๋ฅผ ๋„์›Œ์™”๋‹ค. express์™€ ๋น„์Šทํ•œ apollo-server๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค.

apollo-server ์„ค์น˜ํ•˜๊ธฐ

 yarn add apollo-server graphql

apollo-server ๋ฌธ๋ฒ• ๊ตฌ์„ฑ๊ณผ express์ฐจ์ด

// index.js

// const { ApolloServer, gql } = require('apollo-server');
import { ApolloServer, gql } from "apollo-server";

const typeDefs = gql` // TypeDefs => swagger๋ฅผ ํ†ตํ•ด ์ง์ ‘ ์ž‘์„ฑํ•˜์˜€์ง€๋งŒ Graphqldms typeDefs๋ฅผ ํ†ตํ•ด ์ž๋™์ƒ์„ฑํ•ด์ค€๋‹ค..
  type Query {
    qqq: String
  }
`;

const resolvers = {
  Query: {
    qqq: () => {
      return "Hello World!";
    },
  },
};
====> resolver ๋Š” express ์˜ get API ์™€ ๋น„์Šทํ•˜๋‹ค.
// express
app.get('/', function (req, res) {
  res.send('Hello Wordld')
})

const app = new ApolloServer({
  typeDefs: typeDefs,
  resolvers: resolvers,
	cors: true,  // ๋ชจ๋“  ์‚ฌ์ดํŠธ ํ—ˆ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ
 // cors: { origin: ["https://naver.com", "https://daum.net"] } // ํŠน์ • ์‚ฌ์ดํŠธ๋งŒ ์ง€์ •ํ•˜๊ณ  ์‹ถ์„ ๋•Œ

});

app.listen(3000).then(() => {
  console.log("๋ฐฑ์—”๋“œ API ์„œ๋ฒ„๊ฐ€ ์ผœ์กŒ์–ด์š”!!!");
});
================================================

// express
app.listen(3000)

// apollo-server
app.listen(port).then(({ url }) => {
  console.log(`๐Ÿš€ Server ready at ${url}`);
});

๐Ÿง6์ผ์ฐจ Node.js

๐ŸคCallback

callback ์ด๋ž€?

  • ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์‚ฌ์šฉ ๋˜๋Š” ํ•จ์ˆ˜
  • ์ด๋ฒคํŠธ์— ์˜ํ•ด ํ˜ธ์ถœ๋˜์–ด์ง€๋Š” ํ•จ์ˆ˜
function add (x, y, callback){
	let result = x * y
	callback(result)
}

function result (data) {
	console.log(data, "์ฝœ๋ฐฑํ•จ์ˆ˜ ์‹คํ–‰")
}

add(5, 10, result)

add ํ•จ์ˆ˜์—๋Š” ์ธ์ž๋กœ x,y,callback์ด ๋“ค์–ด๊ฐ€๊ณ  callback ์—๋Š” ํ•จ์ˆ˜ ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.

addํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด add์•ˆ์— ์ธ์ž๋กœ ๋“ค์–ด๊ฐ€ result๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

์ฆ‰, addํ•จ์ˆ˜ ์ธ์ž๋กœ ์‚ฌ์šฉ๋œ result ํ•จ์ˆ˜๋ฅผ callback ํ•จ์ˆ˜๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

๐Ÿ™์ด๋ฒคํŠธ์— ์˜ํ•ด ํ˜ธ์ถœ๋˜์–ด์ง€๋Š” ํ•จ์ˆ˜

<button onClick={handleClickFunction}></button>

button ์•ˆ์— onClick ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ณ  onClick ์—์„  handleClickFunction ์„ ์‹คํ–‰ํ•œ๋‹ค.

์ฆ‰, onClick์ด๋ผ๋Š” ์ด๋ฒคํŠธ ํ•จ์ˆ˜ ์•ˆ์— handleClickFunction๊ฐ€ ๋“ค์–ด๊ฐ€ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์—

handleClickFunction๊ฐ€ callback ํ•จ์ˆ˜๋ผ๊ณ  ๋ถ€๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

callback ๋ฐฉ์‹!!

  1. ๋™๊ธฐ์  ํ•จ์ˆ˜: ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์ฝ”๋“œ๊ฐ€ ์™„๋ฃŒํ•œ ํ›„ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰
  2. ๋น„๋™๊ธฐ์  ํ•จ์ˆ˜: ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์ฝ”๋“œ์˜ ์™„๋ฃŒ ์—ฌ๋ถ€์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ฆ‰์‹œ ๋‹ค์Œ ์ฝ”๋“œ๋กœ ๋„˜์–ด๊ฐ€์„œ ์‹คํ–‰

callback ์ง€์˜ฅ๊ณผ promise ๋Š” ๋…ธ์…˜ ๋ณด์ถฉ ์ž๋ฃŒ ์ฐธ๊ณ !!!

async/await

axios๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ async/await ๋ฅผ ์ง€์›ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

async function getData(url){
  let data = (await axios.get(url)).data;
	return data;
  }

let url = 'https://koreanjson.com/posts/1';

async function dataArr(){
    let data = await getData(url);
    let result = [];
    for(let v of Object.values(data)){
        result.push(v);
    }
    console.log(result);
}

dataArr();

async

: async ํ‚ค์›Œ๋“œ๋Š” function ์•ž์— ์‚ฌ์šฉํ•˜๊ณ  async๊ฐ€ ๋ถ™์€ ํ•จ์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ Promise๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ ,

ํ”„๋ผ๋ฏธ์Šค๊ฐ€ ์•„๋‹Œ ๊ฒƒ์€ ํ”„๋ผ๋ฏธ์Šค๋กœ ๊ฐ์‹ธ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

await

: async ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ๋™์ž‘ํ•œ๋‹ค. Promise๊ฐ€ ์ฒ˜๋ฆฌ๋ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

๋™๊ธฐ ๋น„๋™์‹ ๋ฐฉ์‹์œผ๋กœ ํ•จ์ˆ˜ ์‹คํ–‰

import axios from 'axios';

// 1. ๋น„๋™๊ธฐ๋ฐฉ์‹
function fetchAsync() {
  const result = axios.get("https://koreanjson.com/posts/1");
  console.log("๋น„๋™๊ธฐ๋ฐฉ์‹: ", result); // Promise { <pending> }
}

fetchAsync();

// 2. ๋™๊ธฐ๋ฐฉ์‹
// async function fetchSync() {
//   //  =>   ํ•จ์ˆ˜ ์ค‘๋ณต ์„ ์–ธ ๋ฌธ์ œ๋ฅผ ํ”ผํ•˜์ž!!! (ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝ)
//   const result = await axios.get("https://koreanjson.com/posts/1");
//   console.log("๋™๊ธฐ๋ฐฉ์‹: ", result.data); // ์ œ๋Œ€๋กœ๋œ ๊ฒฐ๊ณผ => { title: "....." }
// }
const fetchSync = async () => {
  const result = await axios.get("https://koreanjson.com/posts/1");
  console.log("๋™๊ธฐ๋ฐฉ์‹: ", result); // ์ œ๋Œ€๋กœ๋œ ๊ฒฐ๊ณผ => { title: "....." }
  console.log("๋™๊ธฐ๋ฐฉ์‹: ", result.data.title);
};

fetchSync();
profile
๊ฐ„์ ˆํ•˜๊ณ  ์น˜์—ดํ•˜๊ฒŒ ์‚ด์ž
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€