ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

javascript์˜ ๊ฐ์ฒด ์ค‘ Promise ๊ฐ์ฒด๋Š” ๋น„๋™๊ธฐ ์ž‘์—…์ด ๋งž์ดํ•  ๋ฏธ๋ž˜์˜ ์™„๋ฃŒ ๋˜๋Š” ์‹คํŒจ์™€ ๊ทธ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

MDN์˜ ์„ค๋ช…์œผ๋กœ ์•„๋ž˜์™€๊ฐ™์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์š”์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[์ถœ์ฒ˜] - https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Promise

Promise๋Š” ๋‹ค์Œ ์ค‘ ํ•˜๋‚˜์˜ ์ƒํƒœ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

  • ๋Œ€๊ธฐ(pending): ์ดํ–‰ํ•˜์ง€๋„, ๊ฑฐ๋ถ€ํ•˜์ง€๋„ ์•Š์€ ์ดˆ๊ธฐ ์ƒํƒœ.
  • ์ดํ–‰(fulfilled): ์—ฐ์‚ฐ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋จ.
  • ๊ฑฐ๋ถ€(rejected): ์—ฐ์‚ฐ์ด ์‹คํŒจํ•จ.

๋Œ€๊ธฐ ์ค‘์ธ ํ”„๋กœ๋ฏธ์Šค๋Š” ๊ฐ’๊ณผ ํ•จ๊ป˜ ์ดํ–‰ํ•  ์ˆ˜๋„, ์–ด๋–ค ์ด์œ (์˜ค๋ฅ˜)๋กœ ์ธํ•ด ๊ฑฐ๋ถ€๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดํ–‰์ด๋‚˜ ๊ฑฐ๋ถ€๋  ๋•Œ, ํ”„๋กœ๋ฏธ์Šค์˜ then ๋ฉ”์„œ๋“œ์— ์˜ํ•ด ๋Œ€๊ธฐ์—ด(ํ)์— ์ถ”๊ฐ€๋œ ์ฒ˜๋ฆฌ๊ธฐ๋“ค์ด ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ ์ดํ–‰ํ–ˆ๊ฑฐ๋‚˜ ๊ฑฐ๋ถ€๋œ ํ”„๋กœ๋ฏธ์Šค์— ์ฒ˜๋ฆฌ๊ธฐ๋ฅผ ์—ฐ๊ฒฐํ•ด๋„ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ, ๋น„๋™๊ธฐ ์—ฐ์‚ฐ๊ณผ ์ฒ˜๋ฆฌ๊ธฐ ์—ฐ๊ฒฐ ์‚ฌ์ด์— ๊ฒฝํ•ฉ ์กฐ๊ฑด์€ ์—†์Šต๋‹ˆ๋‹ค.

 

javascript์˜ ์–ธ์–ดํŠน์ง•์ธ ๋น„๋™๊ธฐ์„ฑ๋•Œ๋ฌธ์— Promise๊ฐ์ฒด๋ฅผ ๋งŽ์ด์“ฐ๊ฒŒ๋˜๋Š”๋ฐ ๊ฐ๊ฐ ํ•˜๋‚˜์˜ Promise๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ํฌ๊ฒŒ ๊ณ ๋ฏผํ• ๊ฒŒ ์—†๋Š”๋ฐ, ์ž‘์—…์„ ํ•˜๋‹ค๋ณด๋‹ˆ ์—ฌ๋Ÿฌ๊ฐœ์˜ Promise ๊ฐ์ฒด๋ฅผ ์‹คํ–‰ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค! ์ฒจ์—” ๋ณ„ ์ƒ๊ฐ์—†์ด Promise ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ๋ฐฐ์—ด์— ๋„ฃ๊ณ  Promise ๊ธฐ๋ณธํ•จ์ˆ˜์— ์žˆ๋Š” Promise.all ํ˜น์€ Promise.allsettled ํ•จ์ˆ˜๋ฅผ ์“ฐ๋ฉด ํ•ด๊ฒฐ๋ ๊ฑฐ๋ผ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ๋“ค๋ฉด ์ด๋Ÿฐ์‹์œผ๋กœ ๋ง์ด์ฃ .

let p1 = new Promise((resolve, reject)=>{
...
})
let p2 = new Promise((resolve, reject)=>{
...
})
Promise.allSettled([p1, p2])

์ฐธ๊ณ ๋กœ Promise.all๊ณผ Promise.allSettled์˜ ์ฐจ์ด๋Š” ๋ชจ๋“  Promise๊ฐ์ฒด๋ฅผ ์„ฑ๊ณต์‹œ์ผœ์•ผ ์ธ์ •ํ• ๊ฑด์ง€ ์•„๋‹ˆ๋ฉด ์„ฑ๊ณตํ•˜๋Š” Promise์™€ ์‹คํŒจํ•˜๋Š” Promise๋ฅผ ์—ผ๋‘์— ๋‘๊ณ  ์‹คํ–‰์‹œํ‚ฌ๊ฑด์ง€์˜ ์ฐจ์ด์ธ๋ฐ, all์€ ํ•˜๋‚˜๋ผ๋„ ์‹คํŒจํ•˜๋ฉด ์‹คํŒจ๊ฐ€ ๋˜๊ณ , allSettled๋Š” 10๊ฐœ์ค‘์— ํ•˜๋‚˜๊ฐ€ ์‹คํŒจํ•ด๋„ ์ผ๋‹จ ์ „๋ถ€ ์‹คํ–‰์€ ํ•œ๋‹ค๋ผ๋Š” ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

let promises = []
for(const requestUrl of requestUrls) {
  promises.push(await this.util.httpRequest(url, 'GET'))
}
Promise.allSettled(promises)
for (const result of results) {
    if(result.status === 'fulfilled') {
    // ... ์„ฑ๊ณต
    } else {
    // ... ์‹คํŒจ
    }
}

์•„๋ฌดํŠผ๊ฐ„ ์ €๋ ‡๊ฒŒ Promise๊ฐ€ ์ ์„๋•Œ๋Š” ์ƒ๊ด€์ด ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทผ๋ฐ ์ €๋Š” ์•ฝ 1500๊ฐœ ์ •๋„์˜ Promise๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ ค ํ–ˆ๋Š”๋ฐ ์—๋Ÿฌ๊ฐ€ ๋œจ๋”๊ตฐ์š”.

http request๋ฅผ ํ•˜๋Š” ๊ณต์šฉํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ  ์—ฌ๋Ÿฌ๊ฐœ์˜ url์— 'GET' ์œผ๋กœ ํ˜ธ์ถœํ•˜๊ณ  ์‘๋‹ต๊ฐ’์„ ๋ฐ›์•„์˜ค๋Š” ์ƒํ™ฉ์ด์—ˆ๋Š”๋ฐ ํ•œ๋ฒˆ์— ๋„ˆ๋ฌด ๋งŽ์€ ์–‘์„ ํ˜ธ์ถœํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๋ ค๊ณ ํ•˜๋‹ˆ Promise๊ด€๋ จ๋œ ์—๋Ÿฌ์™€ request๋ฅผ ๋ฐ›๋Š” OPEN API ์ชฝ์—์„œ๋„ ์—๋Ÿฌ๋ฅผ ๋‚ด๋”๊ตฐ์š”.. ์ฐธ๊ณ ๋กœ ์ €๋Š” ๊ธฐ์ƒ์ฒญ์˜ OPEN API๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ์‚ฝ์ง‘์„ ํ•˜๋˜์ค‘, Promise ์˜ ์‹คํ–‰์„ ์ œ์–ดํ•œ๋‹ค๋Š” limit๊ด€๋ จ ๋ชจ๋“ˆ๋“ค์ด ์žˆ๋Š”๊ฑธ๋กœ ๋ณด์•„ ์ผ๋‹จ Promise์˜ ์‹คํ–‰๊ฐœ์ˆ˜๋ฅผ ์ค„์—ฌ์•ผํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ณ , npm์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ๋“ค์„ ์‚ฌ์šฉํ• ๊นŒ ํ•ด๋ดค์ง€๋งŒ javascript ๋‚ด๋ถ€ํ•จ์ˆ˜๋กœ๋„ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์•„์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊ฟ”๋ดค์Šต๋‹ˆ๋‹ค.

while(this.requestUrls.length) {
    const results = await Promise.allSettled(this.requestUrls.splice(0, 100).map((url)=>this.util.httpRequest(url, 'GET')))
    for await(const result of results) {
        if(result.status === 'fulfilled') {
        // ... ์„ฑ๊ณต
        } else {
        // ... ์‹คํŒจ
        }
    }
}

๊ฐœ์ˆ˜๋ฅผ ์ค„์ธ๋‹ค๊ณ ํ•ด์„œ ๋ชจ๋“  Promise๊ฐ€ fulfiled์˜ ์ƒํƒœ, ์ฆ‰ ์„ฑ๊ณตํ•œ๋‹ค๋Š” ๋ณด์žฅ์€ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ์œ„์™€๊ฐ™์€ ๊ฒฝ์šฐ๋Š” requestUrls์˜ ๋ฐฐ์—ด์—์„œ 100๊ฐœ์”ฉ ์ถ”์ถœํ•˜์—ฌ promise๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๊ณ  ๊ทธ๋‹ค์Œ์— promise.allSettled ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํ–‰ํ•˜๋Š” ๊ตฌ์กฐ๋กœ ๋ฐ”๊พธ๋‹ˆ ์ผ๋‹จ Promise์˜ ๊ด€๋ จํ•œ ์—๋Ÿฌ๋Š” ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  http ํ†ต์‹ ์— ๋Œ€ํ•œ ์—๋Ÿฌ๋Š” ์ข…์ข… ๋‚˜๊ธฐ์— ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๊ธด ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

Node.js์˜ Worker๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํผํฌ๋จผ์Šค๋ฅผ ๋” ๋†’์ผ ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค๋งŒ ์ƒ๊ฐ๋ณด๋‹ค OPEN API ์ชฝ์—์„œ๋„ ์‘๋‹ต์—๋Ÿฌ๊ฐ€ ๋งŽ์ด ๋ฐœ์ƒํ•˜์—ฌ ์ผ๋‹จ ์ด์ •๋„๋กœ ๊ตฌํ˜„ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

'javascript > ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

9. generators  (0) 2019.01.22
8. iterators + for..of  (0) 2019.01.11
7. let + const  (0) 2019.01.06
6. default + rest + spread  (0) 2019.01.03
5. destructuring  (0) 2018.12.31
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ