Modern Javascript Deep Dive(45) - Promise

Promise์™€ async/await
Feb 26, 2024
Modern Javascript Deep Dive(45) - Promise
Contents
Promise

Promise

 
๐Ÿ’ก
ํ•ด๋‹น ๊ฒŒ์‹œ๋ฌผ์€ Javascript Deep Dive๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์„ฑ ๋์Šต๋‹ˆ๋‹ค.
 
์ „ํ†ต์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
ES6์—์„  ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋‚˜ ๋™์ž‘์˜ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ํŒจํ„ด์œผ๋กœ ES6์—์„  Promise๋ผ๋Š” ๊ฐœ๋…์„ ๋„์ž…ํ–ˆ๋‹ค.
 
๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ž€ ๋ฌด์—‡์ผ๊นŒ?
Javascript Deep Diev์—์„  ์ด๋ ‡๊ฒŒ ๋งํ•œ๋‹ค.
๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ๋น„๋™๊ธฐ๋กœ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์™„๋ฃŒ๋˜์ง€ ์•Š์•˜๋‹ค๊ณ  ํ•ด๋„ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ์ฆ‰์‹œ ์ข…๋ฃŒ๋œ๋‹ค. ์ฆ‰, ๋น„๋™๊ธฐ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ๋น„๋™๊ธฐ๋กœ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋น„๋™๊ธฐ ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋œ ์ดํ›„์— ์™„๋ฃŒ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ๋น„๋™๊ธฐ๋กœ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ์—์„œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์™ธ๋ถ€๋กœ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜ ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋ฉด ๊ธฐ๋Œ€ํ•œ ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.
 
 

Persistent Issues of Asynchronous Processing

 
  1. ๋น„๋™๊ธฐ ๋™์ž‘์„ ์ œ์–ดํ•  ์ˆ˜ ์—†์Œ
setTimeout ํ•จ์ˆ˜๋Š” ๋น„๋™๊ธฐ๊ณ  ๋น„๋™๊ธฐ์ธ ์ด์œ ๋Š” callback ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ์ด ๋น„๋™๊ธฐ๋กœ ๋™์ž‘ํ•œ๋‹ค.
let g = 0; setTimeout(() => { g = 100; }, 0); console.log(g); // 0
์•„๋ž˜์ชฝ console์ด 100์„ ๊ฐ€๋ฆฌํ‚ฌ ๊ฒƒ์„ ์˜ˆ์ƒํ•˜๊ณ  ์ž‘์„ฑํ–ˆ๊ฒ ์ง€๋งŒ, setTimeout์€ ๋น„๋™๊ธฐ ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— callback ํ•จ์ˆ˜๋Š” ๋™์ž‘์ด ์™„๋ฃŒ๋œ ์‹œ์ ์ธ console.log(g)๊ฐ€ ์‹คํ–‰๋œ ํ›„์— ๋‚ด๋ถ€ callback ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ๋‹ค.
 
  1. callback ์ง€์˜ฅ
    1.  
      ๋˜ ์ „ํ†ต์˜ Javascript์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋•Œ๋ฌธ์— callback ์ง€์˜ฅ์ด ๋ฐœ์ƒํ•˜๊ณค ํ–ˆ๋Š”๋ฐ, ์š”์ฆ˜ ES6์˜ async/await ๋ฌธ๋ฒ•์ด ๋ณดํŽธํ™”๋œ ์ดํ›„๋กœ ๊ฐœ๋ฐœ์— ์ž…๋ฌธํ•œ ์‚ฌ๋žŒ์ด๋ผ๋ฉด ๊ฒช์ง€ ๋ชปํ–ˆ์„ ๊ฒƒ์ด๋‹ค.
      function stepOne(callback) { setTimeout(function () { console.log("Step One completed"); callback(); }, 3000); } function stepTwo(callback) { setTimeout(function () { console.log("Step Two completed"); callback(); }, 3000); } stepOne(function () { stepTwo(function () { console.log("All steps completed"); }); }); // Step One completed // Step Two completed // All steps completed
์ด๋Ÿฐ์‹์œผ๋กœ, callback์˜ ์ˆœ์„œ ๋ณด์žฅ์„ ์œ„ํ•ด ๋น„๋™๊ธฐ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด callback ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ณ โ€ฆ ๋˜ ์‹คํ–‰์ด ์™„๋ฃŒ๋˜๋ฉด callback์„ ์‹คํ–‰์‹œํ‚ค๊ณ .. ๋์—†๋Š” callback ์ง€์˜ฅ์— ๋น ์ง€๊ฒŒ ๋œ๋‹ค.
 
  1. Error Handling์˜ ์–ด๋ ค์›€
    1.  
      try { setTimeout(() => { throw new Error("ERROR!"); }, 1000); } catch (e) { console.log("CATCH ERROR:", e); } // Error: ERROR!
       
      ์œ„ ๊ฐ•์ œ๋กœ ๋ฐœ์ƒ์‹œํ‚จ ์—๋Ÿฌ๋Š” catch ๋ธ”๋ก์—์„œ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.
       
      setTimeout ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ํ•จ์ˆ˜์˜ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  call stack์— push๋œ ํ›„ ์‹คํ–‰๋œ๋‹ค. setTimeout์€ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๊ธฐ ๋•Œ๋ฌธ์— callback ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ๊ฒƒ์„ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ์ฆ‰์‹œ ์ข…๋ฃŒ๋œ ํ›„ ์ฝœ์Šคํƒ์—์„œ ์ œ๊ฑฐ๋œ๋‹ค.
      ์„ค์ •ํ•ด ๋‘” 1000ms์ธ 1์ดˆ๊ฐ€ ์ง€๋‚˜๊ณ  ๋‚˜๋ฉด, setTimeout ํ•จ์ˆ˜์˜ callback ํ•จ์ˆ˜๋Š” task ํ๋กœ push๋œ ํ›„ call stack์ด ๋น„์–ด์กŒ์„ ๋•Œ Event loop์— ์˜ํ•ด call stack์œผ๋กœ push๋˜์–ด ์‹คํ–‰๋œ๋‹ค.
       
      callback ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋Š” ์‹œ์ ์— ์ด๋ฏธ, ํ˜ธ์ถœ์ž์ธ setTimeout ํ•จ์ˆ˜๋Š” call stack์—์„œ ์ œ๊ฑฐ๋œ ์ƒํƒœ๋‹ค. ์ด ๋ง์€ callback์˜ ํ˜ธ์ถœ์ž(caller)๊ฐ€ setTimeout์ด ์•„๋‹ˆ๋ผ๋Š” ์†Œ๋ฆฌ์ด๋ฉฐ, ํ˜„์žฌ ์‹คํ–‰๋œ ํŒŒ์ผ์˜ ์ปจํ…Œ์ŠคํŠธ์˜ ํ•˜์œ„ ์ปจํ…์ŠคํŠธ๊ฐ€ setTimeout์˜ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ์—ฌ์•ผ ํ•˜๋Š”๋ฐ callback์ด ํฌํ•จ๋˜์ง€ ์•Š์•˜๋‹ค๋Š” ๊ฒƒ์€ setTimeout์€ ํ•˜์œ„ ์‹คํ–‰์ปจํ…์ŠคํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์ด๋‹ค.
       
      ์—๋Ÿฌ๋Š” ํ˜ธ์ถœ์ž ๋ฐฉํ–ฅ์œผ๋กœ ์ „ํŒŒ๋œ๋‹ค. ๊ธฐ์กด ํ•จ์ˆ˜์˜ ์‹คํ–‰์ปจํ…์ŠคํŠธ(setTimeout)๊ฐ€ ํ˜ธ์ถœ์ž๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— catch block์— ํฌํ•จ๋˜์ง€ ์•Š์€ ๊ฒƒ์ด๋‹ค.
       
      ๋‹จ์ˆœํ•˜๊ฒŒ ๋งํ•˜๋ฉด, ์‹คํ–‰์ด ์™„๋ฃŒ๋˜๋Š” ์‹œ์ ์— callback์ด ์‹คํ–‰๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— catch์—์„œ ์ธ์‹ํ•˜์ง€ ๋ชปํ•œ ๊ฒƒ์ด๋‹ค.
 

Promise

 
promise๋Š” ES6์—์„œ ๋„์ž…๋œ ๋ฌธ๋ฒ•์ด๋‹ค.
 
์‹ค์ œ๋กœ stackoverflow๋‚˜ reddit ๊ฐ™์€ ์‚ฌ์ดํŠธ๋“ค์˜ ๋งค์šฐ ์˜ค๋ž˜๋œ 8๋…„ ์ด์ƒ๋œ ๊ณผ๊ฑฐ ๊ฒŒ์‹œ๋ฌผ๋“ค์„ ๋ณด๋ฉด ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์— ๊ด€ํ•œ ์งˆ๋ฌธ์ด ์ƒ๋‹นํžˆ ๋งŽ๊ณ , 2018๋…„์ด๋‚˜ 2019๋…„ ์ฏค์—๋Š” async / await์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์งˆ๋ฌธ์ด ๋˜ ๋งŽ๋‹ค. ๊ทผ๋ž˜์—๋Š” ES6์ธ Arrow function์ด๋‚˜ async / await ๋“ฑ promise ๋ฌธ๋ฒ•๋“ค์ด ๋ณดํŽธํ™”๋˜์–ด ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€๋งŒ, ๊ณผ๊ฑฐ์˜ Javascript ์ƒํƒœ๊ณ„๋Š” ์•„์ฃผ ํ˜ผ๋ž€์Šค๋Ÿฌ์› ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
 
  1. Promise ์ƒ์„ฑ์ž
    1.  
      Promise ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ callback ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
      ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์‹คํ–‰๋˜๋ฉด resolve ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ์‹คํŒจํ•˜๋ฉด reject ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
       
      const promise = new Promise((resolve, reject) => { const success = true; if (success) { return resolve("Operation successful"); } else { return reject("Operation failed"); } }); console.log(promise); // Promise { <pending> }
       
      Promise๋Š” ๋‹ค์Œ์˜ ์ƒํƒœ๋กœ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•œ๋‹ค.
      • pending
        •  
          ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์•„์ง ์ˆ˜ํ–‰๋˜์ง€ ์•Š์€ ์ƒํƒœ, Promise๊ฐ€ ์ƒ์„ฑ๋œ ์งํ›„
       
      • fulfilled
        •  
          ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰๋œ ์ƒํƒœ(์„ฑ๊ณต), Resolve ํ•จ์ˆ˜ ํ˜ธ์ถœ
       
      • rejected
        •  
          ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰๋œ ์ƒํƒœ(์‹คํŒจ), Reject ํ•จ์ˆ˜ ํ˜ธ์ถœ
       
      fulfilled ๋˜๋Š” rejected๋œ ์ƒํƒœ๋ฅผ settled ์ƒํƒœ๋ผ๊ณ  ํ•œ๋‹ค. settled ์ƒํƒœ๋กœ ๋ณ€ํ™”ํ•œ ํ›„์—๋Š” ๋‹ค๋ฅธ ์ƒํƒœ๋กœ ๋ณ€ํ™”ํ•  ์ˆ˜ ์—†๋‹ค.
       
function stepOne(callback) { setTimeout(function() { console.log("Step One completed"); callback(); }, 3000); } function stepTwo(callback) { setTimeout(function() { console.log("Step Two completed"); callback(); }, 3000); } function stepThree(callback) { setTimeout(function() { console.log("Step Three completed"); callback(); }, 3000); } stepOne(function() { stepTwo(function() { stepThree(function() { console.log("All steps completed"); }); }); });
  1. ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ
    1.  
      • then
        •  
          ์ฒซ ๋ฒˆ์งธ callback ํ•จ์ˆ˜๋Š” Promise๊ฐ€ ์„ฑ๊ณต ํ–ˆ์„ ๋•Œ, ๋‘ ๋ฒˆ์งธ callback์€ Promise๊ฐ€ ์‹คํŒจ ํ–ˆ์„ ๋•Œ ๋ฐ›๋Š”๋‹ค.
          const promise = new Promise((resolve, reject) => { const success = true; if (success) { return resolve("Operation successful"); } else { return reject("Operation failed"); } }).then((v) => { console.log("Resolve:", v); }, (e) => { console.log("Reject:", e); }); // Resolve: Operation successful const promise = new Promise((resolve, reject) => { const success = false; if (success) { return resolve("Operation successful"); } else { return reject("Operation failed"); } }).then((v) => { console.log("Resolve:", v); }, (e) => { console.log("Reject:", e); }); // Reject: Operation failed
       
      • catch
        •  
          Promise๊ฐ€ rejected๋œ ์ƒํƒœ์ธ ๊ฒฝ์šฐ ํ˜ธ์ถœ๋˜๋ฉฐ, then๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์–ธ์ œ๋‚˜ Promise๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์—๋Ÿฌ ํ•ธ๋“ค๋ง์„ ์œ„ํ•ด ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
           
      • finally
        •  
          Promise์˜ fulfilled๋‚˜ rejected์™€ ์ƒ๊ด€์—†์ด ๋ฌด์กฐ๊ฑด ํ•œ ๋ฒˆ ํ˜ธ์ถœ๋œ๋‹ค. ์–ธ์ œ๋‚˜ Promise๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
       
       
      ์ด๋Ÿฌํ•œ callback ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ๋Š”, ์ฝ”๋“œ ์ œ์–ด์— ์ง€๋Œ€ํ•œ ์˜ํ–ฅ์„ ๋ผ์ณค์ง€๋งŒ ์ด ๋˜ํ•œ callback ํŒจํ„ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋…์„ฑ์ด ์ข‹์ง€ ์•Š๋‹ค.
       
  1. Promise Static ๋ฉ”์„œ๋“œ
    1.  
    2. Promise.resolve / Promise.reject
      1.  
        const resolve = Promise.resolve([1,2,3] resolve.then(console.log) // [1,2,3] const reject = Promise.reject(new Error('Error!')); reject.catch(console.log) // Error: Error!
         
        ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’์„ resolve๋‚˜ reject๋กœ ๋ž˜ํ•‘ํ•˜์—ฌ Promise๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฌธ๋ฒ•์ด๋‹ค.
       
    3. Promise.all
      1.  
        ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ๋ชจ๋‘ ๋ณ‘๋ ฌ๋กœ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
        const promise1 = () => { return new Promise(resolve => setTimeout(() => { resolve(1) }, 3000)) }; const promise2 = () => { return new Promise(resolve => setTimeout(() => { resolve(2) }, 2000)) } const promise3 = () => { return new Promise(resolve => setTimeout(() => { resolve(3) }, 1000)); }; Promise.all([ promise1(), promise2(), promise3() ]).then((resolve) => { console.log("Resolve:", resolve); }); // Resolve: [ 1, 2, 3 ]
         
        Promise.all์€ Promise๋ฅผ ๊ฐ€์ง„ ๊ฐ’์˜ ๋ฐฐ์—ด ๋“ฑ์˜ ์ดํ„ฐ๋Ÿฌ๋ธ”์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค.
        ์ „๋‹ฌ๋ฐ›์€ ๋ชจ๋“  Promise๊ฐ€ fulfilled ์ƒํƒœ๊ฐ€ ๋˜๋ฉด ๋ชจ๋“  ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅํ•ด ์ƒˆ๋กœ์šด Promise๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
         
        ์ฒซ๋ฒˆ์งธ promise1์ด ์ œ์ผ ๋‚˜์ค‘์— fulfilled ์ƒํƒœ๊ฐ€ ๋œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ Promise.all์€ ์ฒซ ๋ฒˆ์งธ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ์ฒ˜๋ฆฌํ•œ ๊ฒฐ๊ณผ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.
         
        ์ฆ‰, ์ฒ˜๋ฆฌ ์ˆœ์„œ๊ฐ€ ๋ณด์žฅ๋œ๋‹ค.
         
        ํ•œํŽธ Promise๊ฐ€ ํ•˜๋‚˜๋ผ๋„ rejected๊ฐ€ ๋œ๋‹ค๋ฉด ๋‚˜๋จธ์ง€ promise๊ฐ€ fulfilled๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ์ฆ‰์‹œ ์ข…๋ฃŒํ•œ๋‹ค.
         
    4. Promise.race
      1.  
        Promise.all๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋™์ž‘ํ•˜์ง€๋งŒ, ๋ชจ๋“  ํ”„๋กœ๋ฏธ์Šค๊ฐ€ fulfilled ์ƒํƒœ๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๊ฐ€์žฅ ๋จผ์ € fulfilled ์ƒํƒœ๊ฐ€ ๋œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ resolveํ•˜๋Š” ์ƒˆ๋กœ์šด ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
        ... Promise.race([ promise1(), promise2(), promise3() ]).then((resolve) => { console.log("Resolve:", resolve); }); // Resolve: 3
       
    5. Promise.allSettled
      1.  
        Promise๊ฐ€ ๋ชจ๋‘ settled ์ƒํƒœ๊ฐ€ ๋˜๋ฉด ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
        Promise.all์ด ๋ชจ๋‘ resolve๋˜์•ผ ํ•˜๋Š” ๊ฒƒ์„ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ, allSettled๋Š”
        rejected๋œ ์ƒํƒœ๋„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
        ๋งŒ์•ฝ rejected๋œ ๊ฒฝ์šฐ reason property๋ฅผ ๊ฐ€์ง„๋‹ค.
        const promise1 = () => { return new Promise(resolve => setTimeout(() => { resolve(1) }, 3000)) }; const promise2 = () => { return new Promise(resolve => setTimeout(() => { resolve(2) }, 2000)) } const promise3 = () => { return new Promise((resolve, reject) => { return reject(new Error("Reject ERROR")) }); }; Promise.allSettled([ promise1(), promise2(), promise3() ]).then((resolve) => { console.log("Resolve:", resolve); }).then(e => { console.log("Error:", e); }); // Resolve: [ // { status: 'fulfilled', value: 1 }, // { status: 'fulfilled', value: 2 }, // { // status: 'rejected', // reason: Error: Reject ERROR // ... // at node:internal/main/run_main_module:23:47 // } // ] // Error: undefined
       

async/await

async/await์€ Promise ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.
then/catch/finally๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋™๊ธฐ์ ์œผ๋กœ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ Promise์˜ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
const func = () => { const promise = new Promise(resolve => setTimeout(() => { resolve(1) }, 3000)); return promise }; (async() => { const resolve = await func(); console.log("Resolve:", resolve); })();
 
  1. async
    1.  
      await ํ‚ค์›Œ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ async function ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
      async function์€ async ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ์ •์˜ํ•˜๋ฉฐ ์–ธ์ œ๋‚˜ promise๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      async ํ•จ์ˆ˜ inner์˜ ์‹คํ–‰ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹ค์ œ promise๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋”๋ผ๋„, async๋Š” ์–ธ์ œ๋‚˜ ๋ฐ˜ํ™˜๊ฐ’์„ resolveํ•˜๋Š” promise๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 
  1. await
    1.  
      await์€ Promise๊ฐ€ settled ์ƒํƒœ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•˜๋‹ค settled๊ฐ€ ๋˜๋ฉด promise๊ฐ€ resolveํ•œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
       
      await์„ ๋ชจ๋“  ํ‚ค์›Œ๋“œ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ฃผ์˜ํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๋งŒ์•ฝ ์„ ํ–‰ Promise๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๋งŽ์€ ์‹œ๊ฐ„์ด ์†Œ์š”๋œ๋‹ค๋ฉด, ์„ ํ–‰ Promise์˜ ์™„๋ฃŒ ์‹œ์ ์„ ๋‹ค์Œ Promise๊ฐ€ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ ์ด๋Ÿด ๊ฒฝ์šฐ์—๋Š” Promise.all์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
 
  1. Error Handling
    1.  
      async ํ•จ์ˆ˜ ๋‚ด์—์„œ catch ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ error ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด async ํ•จ์ˆ˜๋Š” ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋ฅผ rejectํ•˜๋Š” promise๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 
 
๊ทธ๋ ‡๋‹ค๋ฉด, ๋งˆ์ง€๋ง‰ ์˜ˆ์ œ๋กœ Promise์˜ ์‹คํ–‰ ์ˆœ์„œ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋ฉด
console.log("Start"); // 1 function delay(time) { return new Promise(resolve => setTimeout(resolve, time)); } async function asyncOperation() { await delay(2000); console.log("Async operation completed"); // 5 } const firstPromise = new Promise(resolve => { setTimeout(() => { console.log("First promise resolved"); // 4 resolve(); }, 3000); }); const secondPromise = new Promise(resolve => { setTimeout(() => { console.log("Second promise resolved"); // 3 resolve(); }, 1000); }); async function main() { await firstPromise; await secondPromise; await asyncOperation(); console.log("All promises and async operations completed"); // 6 } main(); console.log("End"); // 2
 
์—ฌ๊ธฐ์„œ ๋“ค ์ˆ˜ ์žˆ๋Š” ์˜๋ฌธ์€, ์™œ firstPromise์˜ setTimeout์˜ callback์ด ๋จผ์ € ์‹คํ–‰์•ˆ๋˜๊ณ , second๊ฐ€ ๋จผ์ € ์‹คํ–‰ ๋๋Š”์ง€ ์ผ ๊ฒƒ์ด๋‹ค.
 
์œ„์—์„œ ์„ค๋ช…ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ์ฝ”๋“œ์˜ ๊ตฌ์กฐ์— ๋”ฐ๋ฅด๋ฉด await firstPromise;๊ฐ€ ๋จผ์ € ์‹คํ–‰๋˜์–ด์•ผ ํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋˜์ง€๋งŒ, secondPromise์˜ setTimeout ํ•จ์ˆ˜์˜ ์‹œ๊ฐ„์ด ๋” ์งง์•„์„œ ํ•ด๋‹น Promise๊ฐ€ ๋จผ์ € ์™„๋ฃŒ๋œ ๊ฒƒ์ด๋‹ค.
 
๋น„๋™๊ธฐ ์—๋Ÿฌ ํ•ธ๋“ค๋ง์˜ ์–ด๋ ค์›€์—์„œ ์„ค๋ช…ํ–ˆ๋˜ ๊ฒƒ ์ฒ˜๋Ÿผ, setTimeout ์ž์ฒด๋Š” ๋น„๋™๊ธฐ ํ•จ์ˆ˜๊ณ  Promise์˜ ์‹คํ–‰์€ ์ด๋ฏธ ์™„๋ฃŒ ๋๊ธฐ ๋•Œ๋ฌธ์—, task queue์— ๋น ์ ธ ์žˆ๋˜ callback ํ•จ์ˆ˜๊ฐ€ call stack์— ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ๊ฒƒ ๋ฟ ์‹คํ–‰ ์ž์ฒด๋Š” ์˜๋„ํ•œ ๋Œ€๋กœ ์‹คํ–‰๋๋‹ค.
 
๋งŒ์•ฝ ์œ„ ์ฝ”๋“œ๋ฅผ ์ •๋ง๋กœ ๋™์ž‘ ์ž์ฒด๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰์‹œํ‚ค๊ณ  ์‹ถ๋‹ค๋ฉด resolve๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›์•„ promise๋ฅผ returnํ•˜๋Š”
console.log("Start"); function delay(time) { return new Promise(resolve => setTimeout(resolve, time)); } async function asyncOperation() { await delay(2000); console.log("Async operation completed"); } const firstPromise = async() => { await delay(3000); console.log("First Promise resolved"); } const secondPromise = async() => { await delay(1000); console.log("Second Promise resolved"); } async function main() { await firstPromise(); await secondPromise(); await asyncOperation(); console.log("All promises and async operations completed"); } main(); console.log("End");
 
delayํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ” ์ค€๋‹ค๋ฉด, Promise ๋ฅผ ์›ํ•˜๋Š”๋Œ€๋กœ ๋™์ž‘์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
 
Share article

vlogue