May 23, 2023
์ต๊ทผ ๊ฐ๋ฐ ์ผ์ ๋ฐ ๊ฐ์ธ ์ฌ์ ์ผ๋ก ์ธํด ํฌ์คํ
์ ์ ํ ํ์ง ๋ชปํ๋ค.
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ๋ ์ผ์์ด๋ ๊ฐ๋ฒผ์ด ์์ฌ๋ผ์ ๋ง ์ธ์ ์์ง๋ง ๊ฐ๋ฐ ๋ธ๋ก๊ทธ๋โฆ
์๋ฌด๋ ๊ฒ๋ ๋ง ์ธ์ง๋ฅด๋ ๊ณต๊ฐ๋ ์๋๊ณ , ๋ค๋ฅธ ์์ ๋ณต๋ถํ๋๊ฑฐ๋ ์ ๋ง ์ซ์ดํด์โฆ
๊ทธ๋์ ๋ ๋ฏธ๋ฃจ์ง ์์๋ ์ถ๋ค. ใ
ใ
ํ์ฐ์งํฉ์ ํจ์น ๋ด์ญ์ด๋ ๊ฐ๋ฐํ ๊ฑธ TIL ์ชฝ์ ์ฌ๋ฆฌ๋ ค๋ค๊ฐ ์ด์ ๋ ๋ฐ๋ก ์นดํ
๊ณ ๋ฆฌ๋ฅผ ๋นผ์ผ ํ ๋ฏ ์ถ๋ค.
์ด๊ฒ๋ ๋ง์ด ๋ฐ๋ ธ๋๋ฐ ์ธ์ ์ ๋์งโฆ
๋ฌดํผโฆ
ํํ๋กญ๊ฒ(?) ๊ฐ๋ฐ์ ํ๋ค๊ฐ ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ๋ฉด์ ๋น๋๊ธฐ ์ฒ๋ฆฌํ๋ค๊ฐ ์กฐ๊ธ ๋งํ ๋ถ๋ถ์ด ์์๋ค.
์ด๋ฒ ํฌ์คํ
์์๋ ๊ทธ ๋ฌธ์ , ๊ทธ๋ฆฌ๊ณ ์กฐ์ฌ, ํด๊ฒฐ๋ฐฉ๋ฒ์ ๊ณต์ ํ๊ณ ์ ํฌ์คํ
์ ๋จ๊ฒจ๋ณธ๋ค.
์ผ๋ฐ์ ์ผ๋ก ๋ฐ๋ณต๋ฌธ ๋ด์์ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ํ๋ ๊ฒฝ์ฐ๋ ๋๋ฌผ๊ฒ ์๋ค.
์๋ฅผ ๋ค์ด ํน์ ํ๋ผ๋ฉํฐ๋ฅผ ๊ฐ์ง๋ url์ ๋ฐ์ดํฐ๋ฅผ ํฌ๋กค๋งํ๊ฑฐ๋,
๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์์ฐจ์ ์ผ๋ก ๋ฝ์์ค๋๋ฑโฆ ๋ช ๊ฐ์ง ์ผ์ด์ค๊ฐ ์๋ค.
๊ทผ๋ฐ while์ด๋ for ๋ฑ๊ณผ ๊ฐ์ ๊ณ ์ ํ ๋ฐฉ์์ธ ๋ฐ๋ณต ์ํ์์๋ ๋น๋๊ธฐ๊ฐ ์ ๋์ํ๋ค.
ํ์ง๋ง ๊ณ ์ฐจํจ์(้ซๆฌกๅฝๆธ)์ map, filter, foreach๋ฑ์์๋ ์ด ๋น๋๊ธฐ๊ฐ ์๊ฐํ ๋๋ก ํ๋ฌ๊ฐ์ง ์๋๋ค.
์ ๊ทธ๋ฐ๊ฑธ๊น?
๊ทธ๋ฅ ๊ธ์กฐํ ์์ ์ฝ๋๋คโฆ
์์ธ์ ์ฌ๊ณ ๋ฅผ ์ฐพ๋ ์ฝ๋๊ฐ ์๋ค๊ณ ํด๋ณด์.
(์ฌ๋ด์ธ๋ฐ ๋ผ๋
์ ธโฆ 6๋ง์ ๋ฐ์ ์ํ๋๋ฐ ์ ๋ง ๋ง์๋ค.)
๊ทธ๋ฆฌ๊ณ ์๋ ์ฝ๋๋ ํ์
์คํฌ๋ฆฝํธ ํ๋ ์ด๊ทธ๋ผ์ด๋์์ ๋์ํ๋ ์ฝ๋๋ค.
๋ง์ฝ ์ง์ typescript ์ฝ๋๋ฅผ ์์ฑํ๋ค๋ฉด ์ผ๋ถ ์ฝ๋์ awaitํค์๋๋ฅผ ๋ถ์ฌ์ผ ํ๋ค.
async function Run() {
//์์ธ ์๋ ์ ์ฅ ์คํ ์ด
const wineStore: { [key: string]: number } = {
taittinger: 10, //๋ผ๋
์ ธ๋ 10๊ฐ
piper_heidsieck: 12, //ํ์ดํผ ํ์ด์ง์ 12๊ฐ
andre_clouet: 21, //์๋๋ ๋๋ฃจ์๋ 21๊ฐ
opus_one: 5, //์คํผ์ค์์ 5๊ฐ
pounamu: 31, //ํธ๋๋ฌด๋ 31๊ฐ
}
//์ฐพ๋ ์๊ฐ
const findTime = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms))
}
//์์ธ ๊ฒ์ฌ
const checkWineCnt = async (wineName: string): Promise<number> => {
const ms = Math.floor(Math.random() * (3 - 1 + 1) + 1) * 1000
console.log('find start... during ms = ', ms)
const cnt = await findTime(ms).then(v => wineStore[wineName])
console.log('[' + wineName + '] count = ' + cnt)
console.log('find end... ')
return cnt
}
checkWineCnt('opus_one')
}
Run()
findTime ํจ์๋ 1~3์ด ๋๋ค์ผ๋ก ์ฐพ๋ ์๊ฐ์ ๋ฐ๋ฆฌ์ธ์ปจ์ผ๋ก ์์ฑํ๋ค.
checkWineCnt ํจ์๋ ์ ๋ฌ๋ฐ์ ์์ธ ์ด๋ฆ์ผ๋ก ๋ช๊ฐ๊ฐ ์๋์ง ์กฐํ๋ฅผ ํ๋ค.
ํ์ค์ ๊ทธ๋ฅ ์์ธ์
๋ฌ ์ด๊ณ ๊บผ๋ด๋ฉด ๋์ง๋ง -_-;;
๋ญ ๊ทธ๋ฅ ์ํ ์์ ๋๊นโฆ๋ ์ต๊น๋ผ ํด๋ ์ํด ๋ฐ๋๋ค.
๋ฌดํผ ์ด๋ ๊ฒ ๋ฐ๋ณต๋ฌธ์ ์์ฐ๋ ์์ ์์ ์์ํด๋ณด๊ฒ ๋ค.
๋ง์ฝ ์คํ ์ด์ ์๋ ๋ชจ๋ ์์ธ์ ์กฐํํด๋ณด๊ณ ์ถ๋ค๋ฉด ์๋์ฒ๋ผ ํ๋จ์ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค.
async function Run() {
const wineList: string[] = Object.keys(wineStore)
const runFunc = async () => {
for (let i = 0; i < wineList.length; i++) {
await checkWineCnt(wineList[i])
}
}
searchAllFunc()
}
Run()
์ด๋ ๊ฒ ํ ๊ฒฝ์ฐ ์ฐ๋ฆฌ๊ฐ ์๊ฐํ ๋๋ก ๋์์ ํ๋ค.
๊ทผ๋ฐ for๋ฅผ ์ฌ์ฉํ๋ฉด ์ฐ๋ฆฌ๊ฐ ์๊ฐํ ๋๋ก ์ ๋์ํ์ง๋ง, ๊ณ ์ฐจํจ์๋ฅผ ํตํด์ ์์
์ ํ๋ฉด ๋ค๋ฅด๊ฒ ์ฒ๋ฆฌ๋๋ค.
์์ ๊ฐ์ ์์
์ ๊ณ ์ฐจํจ์์ธ foreach๋ก ๋ฐ๊พธ๋ฉด ์๋์ ๊ฐ๋ค.
async function Run() {
const wineList: string[] = Object.keys(wineStore)
wineList.forEach(async (item: string) => await checkWineCnt(item))
}
Run()
์ด๋ ๊ฒ ํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์๋์ ๊ฐ๋ค.
์ ํ ๋น๋๊ธฐ๋ก ๋์ํ์ง ์๋๋ค.
์ด์ ๋ ๊ฐ๋จํ๋ค.
๊ณ ์ฐจํจ์์ธ ForEach๋ ๋น๋๊ธฐ๋ฅผ ๊ธฐ๋ค๋ฆฌ์ง ์๋๋ค.
์ข ๋ ์ ํํ ์ด์ผ๊ธฐ ํ๋ฉด ForEach๋ Promise๋ฅผ ์ธ์งํ์ง ๋ชปํ๋ค.
๋ญ ๊ฐ๋จํ๋ค.
1๋ฒ์ ๋งจ ์ฒ์ ์์ ์์ ์ฌ์ฉํ ๋ฐฉ์์ด๊ณ , 2๋ฒ์ ์๋์ ๊ฐ๋ค.
async function Run() {
const forOfLoop = async () => {
for (const [value] of wineList) {
await checkWineCnt(value)
}
}
forOfLoop()
}
Run()
๋ญ ์ข ์ต์ง์ค๋ฝ๊ธด ํ ์ฝ๋์ธ๋ฐ, ์ดํด๋ฅผ ๋๋ ์ฝ๋์ด๋ ๋์ด๊ฐ์.
์ฌ์ค ์ ์ผ ์ข์ ๊ฑด 1๋ฒ์ธ ๊ฒ ๊ฐ๋ค.
๊ตณ์ด ๋น๋๊ธฐ ๋ฃจํ ์ฝ๋๋ฅผ ๋ฐ๋ก ์์ฑํ์ง ์๊ณ , ๊ทธ๋ฅ ๊ทธ ์๋ฆฌ์์ For๋ฅผ ์์ ํ๋ฉด ๋๋๊น?
์ทจํฅ๊ป ์ฐ์.
์ด๋ฒ์ ์์ธ์ ์ด๋ฆ๊ณผ ์๋์ ๋ฌธ์์ด๋ก ํฉ์ณ์ ๋ง๋๋ ๊ฒ์ ํด์ผ ํ๋ค๊ณ ๊ฐ์ ํ์.
๊ฒฐ๊ณผ๋ฌผ์ ๋ฐฐ์ด์ด๊ณ , ์์๋ ์์ธ๋ช
-์๋ ์ด๋ผ๋ ํํ๋ก ๊ฐ๋ค๊ณ ํ์.
์๋ฅผ ๋ค๋ฉด [taittinger-10, โpiper_heidsieck-12โโฆ] ์ด๋ ๊ฒ ๋ง์ด๋ค.
๊ทธ๋ผ ๊ณ ์ฐจํจ์์ธ Map์ ํ์ฉํด์ ๋ณํํ ์ ์๋ค.
์๋์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํ์.
async function Run() {
const wineList: string[] = Object.keys(wineStore)
const result = wineList.map(async (item: string, idx: number) => {
const cnt = await checkWineCnt(wineList[idx])
return item + '-' + cnt
})
console.log('result = ', result)
}
Run()
์ฐ๋ฆฌ๊ฐ ์ํ๋ ๊ฒฐ๊ณผ๋ ์๋ง checkWineCntํจ์๋ฅผ ์ํํ๋ฉด์ find start end ๋งจํธ๊ฐ ๊ฐ๊ฐ ๋จ๊ณ ,
๊ทธ ๋ค์ ๋ง์ง๋ง์ผ๋ก result์ ์ฝ์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋๊ธธ ์ํ ๊ฒ์ด๋ค.
ํ์ง๋ง ๊ฒฐ๊ณผ๋โฆ
๊ทธ๋ฆฌ๊ณ ์ค๊ฐ์ ๋ก๊ทธ๋ฅผ ๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋์ด ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
"result = ", [Promise: {}, Promise: {}, Promise: {}, Promise: {}]
๊ทธ๋ ๋ค.
์ ๋ถ Promiseํํ๋ก ๋ ๋ฐฐ์ด์ด ์๋ค.
์ฆ Map๋ ๋น๋๊ธฐ๋ฅผ ์ฒ๋ฆฌํ์ง ๋ชปํ๋ค๋ ๋ป์ด๋ค.
๋ฌผ๋ก ์๋ Promise๊ฐ ์์ผ๋ ํด๊ฒฐํ ๋ฐฉ๋ฒ์ ์๋ค.
async function Run() {
const wineStore: { [key: string]: number } = {
taittinger: 10,
piper_heidsieck: 12,
andre_clouet: 21,
opus_one: 5,
}
const findTime = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms))
}
const checkWineCnt = async (wineName: string): Promise<number> => {
const ms = Math.floor(Math.random() * (3 - 1 + 1) + 1) * 1000
console.log('find start... during ms = ', ms)
const cnt = await findTime(ms).then(v => wineStore[wineName])
console.log('[' + wineName + '] count = ' + cnt)
console.log('find end... ')
return cnt
}
const wineList: string[] = Object.keys(wineStore)
const mapResult = wineList.map(async (item: string, idx: number) => {
const cnt = await checkWineCnt(wineList[idx])
return item + '-' + cnt
})
const callPromiseAll = async () => {
const data = await Promise.all(mapResult)
console.log('data = ', data)
return data
}
callPromiseAll()
}
Run()
์ ์ฝ๋๋ฅผ ์ํํ๋ฉด ์๋์ฒ๋ผ ํ์ธํ ์ ์๋ค.
๋ณด๋ฉด checkWineCntํจ์์ ์คํ์ ๋น๋๊ธฐ๋ก ๋์ํ์ง ์์ง๋ง, ๊ฒฐ๊ณผ๋ ์ ๋๋ก ๋์จ๋ค.
์ด๋ฒ์ ์๋์ด 20๊ฐ ์ด์์ธ ์์ธ๋ง ๊ฐ์ ธ์ค๊ณ ์ถ๋ค๊ณ ๊ฐ์ ํด๋ณด์.
async function Run() {
const wineStore: { [key: string]: number } = {
taittinger: 10,
piper_heidsieck: 12,
andre_clouet: 21,
opus_one: 5,
}
const findTime = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms))
}
const checkWineCnt = async (wineName: string): Promise<number> => {
const ms = Math.floor(Math.random() * (3 - 1 + 1) + 1) * 1000
console.log('find start... during ms = ', ms)
const cnt = await findTime(ms).then(v => wineStore[wineName])
console.log('[' + wineName + '] count = ' + cnt)
console.log('find end... ')
return cnt
}
const wineList: string[] = Object.keys(wineStore)
const filterResult = wineList.filter(async (item: string, index: number) => {
const cnt = await checkWineCnt(wineList[index])
return cnt >= 20
})
console.log('filterResult = ', filterResult)
}
Run()
์ด๋ ๊ฒ ํ๋ฉด ์๋๋ ๋๋ฃจ์๋ง ๋ฐฐ์ด๋ก ๋ฐํ๋์ด์ผ ํ๋ค.
๊ทผ๋ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์ ์ฒด ๋ค ๋ฐํ๋๋ค.
"filterResult = ", ["taittinger", "piper_heidsieck", "andre_clouet", "opus_one"]
์ด์ ๋ ์ด๊ฒ๋ Map๊ณผ ๋น์ทํ๋ฐfilter์ ์ฝ๋ฐฑ์ Promise๋ฅผ ๋ฐํํ๊ณ , ์ด๋ ์ธ์ ๋ true๋ก ์ธ์ ๋๋ค.
๊ทธ๋์ ์ ์ฒด ๋ค ๋ฐํ๋๋ ๊ฒ์ด๋ค.
์๋ ํด๊ฒฐํ๋ ค๋ฉด ์ฝ๊ฐ ์ฝ๋๊ฐ ๋์ ๋ถํด์ง๋๋ฐ map์ ์ฌ์ฉํด์ ์ฒ๋ฆฌํ ์ ์๋ค.
์๋๋ ํด๊ฒฐ ์ฝ๋๋ค.
async function Run() {
const wineStore: { [key: string]: number } = {
taittinger: 10,
piper_heidsieck: 12,
andre_clouet: 21,
opus_one: 5,
}
const findTime = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms))
}
const checkWineCnt = async (wineName: string): Promise<number> => {
const ms = Math.floor(Math.random() * (3 - 1 + 1) + 1) * 1000
console.log('find start... during ms = ', ms)
const cnt = await findTime(ms).then(v => wineStore[wineName])
console.log('[' + wineName + '] count = ' + cnt)
console.log('find end... ')
return cnt
}
const wineList: string[] = Object.keys(wineStore)
const promiseWineList = wineList.map(
async (item: string, index: number) => await checkWineCnt(wineList[index])
)
const cntWineList = await Promise.all(promiseWineList) //[10, 12, 21, 5]
const filterResult = wineList.filter((item: string, index: number) => {
const cnt = cntWineList[index]
return cnt >= 20
})
console.log('filterResult = ', filterResult)
}
Run()
๊ทธ๋ผ ์๋์ ๊ฐ์ด ์ถ๋ ฅ๋๋ค.
์ฝ๋๊ฐ ์ฝ๊ฐ ์ง์ ๋ถํด์ง๋๋ฐ ์ฒ๋ฆฌ๋ ๋๋ค.
์ด๋ฒ์ ์ ์์ธ๋ค์ ์๋์ ๋ชจ๋ ํฉ์น๋ ๊ฒ์ ํด๋ณด๋ ค ํ๋ค๊ณ ๊ฐ์ ํ์.
๋ญ ๊ทธ๋ฅ for๋ฌธ ์ฌ์ฉํด์ ํ ์ ์์ง๋ง, ๊ณ ์ฐจํจ์ ์ค reduce๋ผ๋ ๊ฒ์ ์ฐ๋ฉด ๋ ํธํ๊ฒ ํ ์ ์๋ค.
์๋๋ ์์ธ ์ดํฉ์ ๊ตฌํ๋ reduce๋ฅผ ์ฌ์ฉํ ์ฝ๋๋ค.
const sum0 = await wineList.reduce(async (sum: number, item: string) => {
const cnt = await checkWineCnt(item)
return (await sum) + cnt
}, 0)
๊ทผ๋ฐ ์ด๋ ๊ฒ ์์ฑํ๋ฉด typescript ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค.
๋ญ๊ฐ ์ฅํฉํ ์๋ฌ๊ฐ ์๋๋ฐ ๊ทธ๋ฅ ์ฝ๊ฒ ์๊ฐํ๋ฉด sum : number๋ผ๋ ๊ฒ์ ์๋ค.
async ํจ์๊ฐ ๋๋ ์๊ฐ ๋ฐํ์ Promise๊ฐ์ผ๋ก ์จ๋ค.
์ ๊ธฐ reduce ํจ์์ ์ฒซ ์ธ์๋ ๋ฐํ ๊ฐ์ธ๋ฐ ๋ณด๋ฉด ์์ผ๋ก number๋ก ๋์ด ์๋ค.
๋ํ ์ด๊ธฐ ๊ฐ๋ Promise๊ฐ ์๋ 0์ผ๋ก ๋์ด ์๋ค.
๊ทธ๋์ ์ ๋ถ๋ถ์ ๊ณ ์น๋ฉดโฆ
const sum = await wineList.reduce(
async (sum: Promise<number>, item: string) => {
const cnt = await checkWineCnt(item)
return (await sum) + cnt
},
Promise.resolve(0)
)
์ด๋ ๊ฒ ๋ฐ๊พธ๋ฉด ์ด์ ์ ์์ ์ผ๋ก ์ถ๋ ฅ์ด ๋๋ค.
์ ์ฝ๋๊ฐ typescript๋ผ์ ๋ฏธ์ฐ์ ๋ฐฉ์ง๊ฐ ๋๊ฑฐ๋ค.
๋ง์ฝ javascript๋ฅผ ์ฌ์ฉํ๊ณ ์์๋๋ผ๋ฉด ์ด์ํ Promise๋ ๊ฒน์ณค์ ๊ฒ์ด๋ค.
์ง๊ธ๊น์ง ๊ณ ์ฐจํจ์์ ์ผ๋ถ์์ ๋น๋๊ธฐ๋ฅผ ์ฌ์ฉํ ๋ ๋ฌธ์ ์ ๊ณผ ํด๊ฒฐ๋ฐฉ๋ฒ์ ์์๋ดค๋ค.
์ฝ๊ฐ ์ต๊น ์์ ๊ฐ ์ข ์์์ง๋งโฆ ๋ฌธ์ ์ ๋ณธ์ง๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ถฉ๋ถํ ์ ๋ฌ๋์์๊ฑฐ๋ผ ๋ฏฟ๋๋ค.
๋ค์ํ๋ฒ ์ ๋ฆฌํ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
์ด์ ๋๋ก๋ง ์๊ณ ์์ผ๋ฉด ์ ์ธ์ ์๋ค.
๊ทผ๋ฐ ๋ชจ๋ฅด๋ฉด ๊ทธ๋ฅ GPTํํ
์์ ๋ฅผ ๋ณด์ฌ๋ฌ๋ผ๊ณ ํ์.
๊ทธ๋ ๋ค๊ณ ์ด์์ฝ๋๋ฅผ ์ฌ๋ฆฌ์ง ๋ง๊ณ โฆ