Promise Async
์์ Promise์ Async๋ ํฌ์คํ
ํ ์ ์ด ์์ผ๋
์์ธํ ์ค๋ช
์ ์๋ตํ๊ณ 4ํธ์์ ์์ฑํ ์ฝ๋๋ฅผ ์ดํด๋ณด๋ฉด
๋ฌธ์ ์ ์ด ํ๋ ์๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ mysql์์ requset(SQL๋ฌธ)๋ฅผ ์ ๋ฌํ๊ณ
SQL๋ฌธ์ ๋ฐ๋ ๋์๊น์ง ๊ธฐ๋ค๋ฆผ์ ์๊ฐ์ Promise๋ก ์ฒ๋ฆฌ๋ฅผ ํ๋๋ฐ
๊ทธ ์ฒ๋ฆฌ์ ๋ํ return๊ฐ์ ํจ์๊ฐ ์ ๋๋ก ๋ฐ์ง ๋ชปํ๋ ๊ฒ์ด๋ค.
์ฌ๊ธฐ์ Promise๋ก ์ฒ๋ฆฌํ๋ค๋ ๋ง์ด ๋ฌด์จ ๋ง์ด๋๋ฉด
์ฝ๋ฐฑํจ์์ฒ๋ฆฌํ๋ค๋ ๋ง์ด๋ค. funcion(err,req,fields){}; ์ด ๋ถ๋ถ!
ํจ์๋ฅผ ๋ง๋ค์ด์ ๊ทธ ์์ pool.query๋ฅผ ํตํด SQL๋ฌธ์ ์์ฒญํ๊ณ ,
๋ฐ๋ก ์ฝ๋ฐฑํจ์๊ฐ ๋์ค๊ฒ ์ฒ๋ฆฌํ๋ค๋ ๋ง์ด๋ค.
๋ฌธ์ ๋ ์ฝ๋ฐฑํจ์๊ฐ ๋ฐํ๋ ๋๊น์ง ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค๋ ๊ฒ์ธ๋ฐ
์ ๋ฌธ์ ๋๋ฉด pool.query๋ฌธ ๋ค๋ก console.log(โvlavlaโ)๊ฐ ์์ด๋
console.log๊ฐ ๋จผ์ ๋์ค๊ณ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋๊ธฐ ๋๋ฌธ์ด๋ค.
์ฐ๋ฆฌ๋ ์ํ๋ ๋์ ์ํ๋๊ฒ ๋์๋๊ธฐ๋ฅผ ์ํ๋ค.
์ด๋ฅผ ์ํ ๋ฐฉ๋ฒ์ผ๋ก Promise๋ก ์ฒ๋ฆฌ๋ฅผ ํ๋ ์ง
Async/Await์ ์จ์ ํด๊ฒฐํ๋ ๊ฒ์ด๋ค.
JavaScript์ ๋์๊ณผ์ ์ CALL STACK๊ณผ
MICROTASK QUEUE, MACROTASK QUEUE๊ฐ ์กด์ฌํ๋๋ฐ
MICROTASK QUEUE๊ฐ ๋จผ์ ์ฒ๋ฆฌ๋๊ณ
MACROTASK QUEUE๊ฐ ์ฒ๋ฆฌ๋๋ ๋ฐฉ์์ผ๋ก ์งํ๋๋ค.
๋ด๊ฐ ์์ฑํ mysql ํจ์๋ MICROTASK QUEUE ์ฌ๊ธฐ ํด๋นํ๋ค.
๋ฌธ์ ๋ console๊ฐ์ ๊ฒฝ์ฐ๋ CALL STACK์ ํด๋น๋ผ ๋ฐ๋ก ์ฒ๋ฆฌ๋๋ค๋ ๊ฒ์ด๋ค.
CALL STACK์ด 0์์, MICROTASK QUEUE๊ฐ 1์์,
MACROTASK QUEUE๊ฐ 2์์์ธ์
์ด๊ณ
๊ฒฐ๊ตญ ์ฐ๋ฆฌ๊ฐ ํ๊ณ ์ํ๋ ๊ฒ์ ์ฝ์คํ์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ ์ํด
Promise๋ Async/Await์ฒ๋ฆฌ๋ฅผ ํ๋ ๊ฒ์ด๋ค.
๊ธฐ์กด ๋ฌธ๋ฒ์ ์์
Async/Await์ ์ฐ์ง ์์ ๋์ ์ฌ์ฉํ ๋์ ๋ฌธ๋ฒ ์ฐจ์ด๋ ์๋์ ๊ฐ๋ค.
Before
const pool = mysql.createPool({
host: "",
user: "admin",
password: "",
port: 3306,
database: "db_name",
});
function getNotes(){
pool.query(`select BIN_TO_UUID(uuid,true) as uuid,title,contents,created from notes);`, function (err, results, fields) {
console.log(results);
});
};
After
const pool = mysql.createPool({
host: "",
user: "admin",
password: "",
port: 3306,
database: "db_name",
})
.promise();
export async function getNotes(){
const [rows] = awit pool.query(`select BIN_TO_UUID(uuid,true) as uuid,title,contents,created from notes);`);
return rows
};
DB์ ๋ํ ํ ์ฟผ๋ฆฌ๋ฅผ ์ ๋ฆฌํ๋ค๋ฉด,
index๋ db ํ ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ณ ์ํ๋ ์์น๊ฐ ์์ ๊ฒ ์๋๊ฐ
๊ทธ ๊ณณ์ module๋ก ํด๋น ์ฟผ๋ฆฌ๋ฅผ ๋ถ๋ฌ์ค๊ณ
์ฌ์ฉํ๋ฉด ๋๋ค.
import express from "express";
import { getNotesAsync, getNoteAsync, addNotesAsync, updateNoteAsync, deleteNoteAsync } from "./database2.js";
const app = express()
const port = 3000
// body ์ฒ๋ฆฌ๋ฅผ ์ํ ๋ฏธ๋ค์จ์ด ์ค์
app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.get('/notes', async (req, res) => {
const result = await getNotesAsync();
res.send(result);
});
app.get('/note/:id', async (req, res, next) => {
try{
const id = req.params.id;
if(!id) throw new Error('400@No Path Parameter');
const result = await getNoteAsync(id);
if(!result) res.send({});
if(result.length === 0) res.send({});
res.send(result[0]);
}
catch(err){
next(err);
}
});
app.get('/notes', async (req, res, next) => {
const body = req.body;
if(!body.title) sendStatus(400);
if(!body.contents) sendStatus(400);
const result = await addNotesAsync(body.title, body.contents);
if(typeof result.affectedRows === "undefined") throw new Error(`400@Not created`);
if(typeof result.affectedRows !== 1) throw new Error(`400@Not created`);
res.sendStatus(201);
});
app.use((err, req, res, next) => {
console.log(err.stack);
res.status(500).send('Something broke!');
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
});
body์ฒ๋ฆฌ
POST๋ฅผ ๋ง๋ค ๋, body๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด์๋ ๋ฏธ๋ค์จ์ด๊ฐ ํ์ํ๋ค.
Express ๊ณต์๋ฌธ์๋ ์ด๋ฅผ ์ํด ๋ ๊ฐ์ง ๋ฏธ๋ค์จ์ด๋ฅผ ์ถ๊ฐํ๋ผ๊ณ ํ๋ค.
app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
์๋ฌ์ฒ๋ฆฌ
Express ๊ณต์๋ฌธ์์ ์๋ฌ ํธ๋ค๋ง์ ๋ํ ๋ถ๋ถ๋ ์ฐธ๊ณ ํ ์ ์๋๋ฐ
์๋์ฒ๋ผ ์ถ๊ฐํด ์ฌ์ฉํ๋ผ ๋ช
์ํ๋ค.
app.use((err, req, res, next) => {
console.log(err.stack);
res.status(500).send('Something broke!');
});