[TIL] 220109

leesyongยท2022๋…„ 1์›” 9์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
144/204
post-thumbnail

๐Ÿ“ ์˜ค๋Š˜ ํ•œ ๊ฒƒ

  1. ์ฝœ๋ฐฑ ํ•จ์ˆ˜

๐Ÿ“š ๋ฐฐ์šด ๊ฒƒ

4. ์ฝœ๋ฐฑ ํ•จ์ˆ˜

1) ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ž€?

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ์ฝ”๋“œ์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งํ•œ๋‹ค.

2) ์ œ์–ด๊ถŒ

(1) ํ˜ธ์ถœ ์‹œ์ 

var count = 0;
var cbFunc = function () {
  console.log(count);
  if (++count > 4) clearInterval(timer);
};
var timer = setInterval(cbFunc, 300);

์ฝœ๋ฐฑ ํ•จ์ˆ˜(cbFunc)์˜ ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ๋ฐ›์€ ํ•จ์ˆ˜(setInterval)๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์ ์— ๋Œ€ํ•œ ์ œ์–ด๊ถŒ์„ ๊ฐ€์ง„๋‹ค.
โ†’ setInterval ํ•จ์ˆ˜๊ฐ€ 0.3์ดˆ ๋’ค cbFunc ํ•จ์ˆ˜ ํ˜ธ์ถœ

(2) ์ธ์ž

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฃผ์ฒด๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์•„๋‹ˆ๋ผ ๊ทธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ๋ฐ›์€ ํ•จ์ˆ˜์ด๋‹ค.
๋”ฐ๋ผ์„œ, ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ธ์ž์— ์–ด๋–ค ๊ฐ’๋“ค์„ ์–ด๋–ค ์ˆœ์„œ๋กœ ๋„˜๊ธธ ๊ฒƒ์ธ๊ฐ€๋Š” ์ „์ ์œผ๋กœ ๊ทธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ๋ฐ›์€ ํ•จ์ˆ˜์—๊ฒŒ ๋‹ฌ๋ฆฐ ๊ฒƒ์ด๋ฏ€๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์ž„์˜๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

(3) this

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋„ ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” this๊ฐ€ ์ „์—ญ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜์ง€๋งŒ, ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ๋ฐ›์„ ์ฝ”๋“œ์—์„œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์— ๋ณ„๋„๋กœ this๊ฐ€ ๋  ๋Œ€์ƒ์„ ์ง€์ •ํ•œ ๊ฒฝ์šฐ์—๋Š”, ๊ทธ ๋Œ€์ƒ์„ ์ฐธ์กฐํ•˜๊ฒŒ ๋œ๋‹ค.

ํ›„์ž์˜ ๊ฒฝ์šฐ, ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ๋ฐ›์„ ์ฝ”๋“œ๋Š” '์ž์ฒด์ ์œผ๋กœ' call/apply ๋ฉ”์„œ๋“œ์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž์— ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this๊ฐ€ ๋  ๋Œ€์ƒ์„ ๋ช…์‹œ์ ์œผ๋กœ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.

์˜ˆ๋กœ์จ map ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ„๋‹จํžˆ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
map ๋ฉ”์„œ๋“œ ์ž์ฒด์ ์œผ๋กœ call(thisArg || window)๋ฅผ ํ†ตํ•ด thisArg ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ this๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

Array.prototype.map = function (callback, thisArg) {
  var mappedArr = [];
  for (var i = 0; i < this.length; i++) {
    var mappedValue = callback.call(thisArg || window, this[i], i, this);
    mappedArr[i] = mappedValue;
  }
  return mappedArr;
}

๋‹ค๋ฅธ ์˜ˆ๋กœ addEventListener๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์•„์˜ˆ call ๋ฉ”์„œ๋“œ์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž์— addEventListener ๋ฉ”์„œ๋“œ์˜ this๋ฅผ ๊ทธ๋Œ€๋กœ ๋„˜๊ฒจ์ฃผ๋„๋ก ์ž์ฒด์ ์œผ๋กœ ์ •์˜๋˜์–ด ์žˆ๋‹ค.

3) ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜๋‹ค

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ์–ด๋–ค ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ „๋‹ฌํ•˜๋”๋ผ๋„ ๊ทธ ๋ฉ”์„œ๋“œ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ๋œ๋‹ค.
์ฆ‰, ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ „๋‹ฌํ•œ ๊ฒฝ์šฐ๋”๋ผ๋„ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ this๋Š” ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ์ „์—ญ ๊ฐœ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค.


4) ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์— ๋‹ค๋ฅธ ๊ฐ’ ๋ฐ”์ธ๋”ฉํ•˜๊ธฐ

๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๊ฐ€ ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ?
map์ด๋‚˜ forEach ์ฒ˜๋Ÿผ ๋ณ„๋„์˜ ์ธ์ž๋กœ this๋ฅผ ๋ฐ›๋Š” ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ์—๋Š” ์—ฌ๊ธฐ์— ์›ํ•˜๋Š” ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋ฉด ๋˜์ง€๋งŒ, ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ์ฃผ๋Š” ํ•จ์ˆ˜์— this์˜ ์ œ์–ด๊ถŒ๋„ ํ•จ๊ป˜ ๋„˜๊ฒจ์ฃผ๊ฒŒ ๋˜๋ฏ€๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์ž„์˜๋กœ ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์— ๋‹ค๋ฅธ ๊ฐ’์„ ๋ฐ”์ธ๋”ฉํ•˜๊ธฐ ์œ„ํ•œ 4๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

(1) this๋ฅผ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ํ• ๋‹น

์ „ํ†ต์ ์œผ๋กœ๋Š” this๋ฅผ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ๋‹ด์•„ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ํ™œ์šฉํ•  ํ•จ์ˆ˜์—์„œ this ๋Œ€์‹  ๊ทธ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ–ˆ๋‹ค.

var obj1 = {
  name: "obj1".
  func: function () {
    var self = this; // this๋ฅผ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ํ• ๋‹น
    return function () {
      console.log(self.name); // ์ฝœ๋ฐฑ ํ•จ์ˆ˜์—์„œ ๊ทธ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉ
    };
  }
};

var callback = obj1.func();
setTimeout(callback, 1000);

(2) this ๋Œ€์‹  ํ•ด๋‹น ๊ฐ์ฒด ์‚ฌ์šฉ

var obj1 = {
  name: "obj1",
  func: function () {
    console.log(obj1.name);
  }
};

setTimeout(obj1.func, 1000);

(1)๋ณด๋‹ค ํ›จ์”ฌ ์ง๊ด€์ ์ด๊ณ  ๊ฐ„๊ฒฐํ•˜์ง€๋งŒ, ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด ๊ฐ€๋ฆฌํ‚ฌ ๊ฐ์ฒด๊ฐ€ obj1์œผ๋กœ ๊ณ ์ •์ด ๋˜์–ด ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ ์žฌํ™œ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

(3) (1)์˜ func ํ•จ์ˆ˜ ์žฌํ™œ์šฉ

var obj1 = {
  name: "obj1",
  func: function () {
    var self = this; // 2. ์—ฌ๊ธฐ์„œ this๋„ obj2
    return function () {
      console.log(self.name); // obj2
    };
  }
};

var obj2 = {
  name: "obj2",
  func: obj1.func,
};
var callback2 = obj2.func(); // 1. ์—ฌ๊ธฐ์„œ func ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ์˜ this๋Š” obj2์ด๋ฏ€๋กœ
setTimeout(callback2, 1500);

var obj3 = { name: "obj3" };
var callback3 = obj1.func.call(obj3); // call ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœ ์ฃผ์ฒด ํ•จ์ˆ˜(obj1.func)๋ฅผ ์ฆ‰์‹œ ์‹คํ–‰
setTimeout(callback3, 1500);

๋ฒˆ๊ฑฐ๋กญ๊ธด ํ•˜์ง€๋งŒ this๋ฅผ ์šฐํšŒ์ ์œผ๋กœ๋‚˜๋งˆ ํ™œ์šฉํ•จ์œผ๋กœ์จ ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ ์›ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ฐ”๋ผ๋ณด๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

(4) bind ๋ฉ”์„œ๋“œ ํ™œ์šฉ

bind ๋ฉ”์„œ๋“œ๋Š” call ๋ฉ”์„œ๋“œ์™€ ๋‹ฌ๋ฆฌ ํ˜ธ์ถœ ์ฃผ์ฒด ํ•จ์ˆ˜๋ฅผ ์ฆ‰์‹œ ์‹คํ–‰์‹œํ‚ค์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด obj1.func์—์„œ setTimeout์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ํ™œ์šฉํ•  ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ return ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

var obj1 = {
  name: "obj1",
  func: function () {
    console.log(this.name);
  }
};

var obj2 = { name: "obj2" };
setTimeout(obj1.func.bind(obj2), 1500);

5) ์ฝœ๋ฐฑ ์ง€์˜ฅ๊ณผ ๋น„๋™๊ธฐ ์ œ์–ด

(1) ์ฝœ๋ฐฑ ์ง€์˜ฅ, ๋น„๋™๊ธฐ์ ์ธ ์ฝ”๋“œ

์ฝœ๋ฐฑ ์ง€์˜ฅ์ด๋ž€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ต๋ช… ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ณผ์ •์ด ๋ฐ˜๋ณต๋˜์–ด ์ฝ”๋“œ์˜ ๋“ค์—ฌ์“ฐ๊ธฐ ์ˆ˜์ค€์ด ๊ฐ๋‹นํ•˜๊ธฐ ํž˜๋“ค ์ •๋„๋กœ ๊นŠ์–ด์ง€๋Š” ํ˜„์ƒ์„ ๋งํ•œ๋‹ค.
์ด๋Š” ์ฃผ๋กœ ๋น„๋™๊ธฐ์ ์ธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์ž์ฃผ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

๋น„๋™๊ธฐ์ ์ธ ์ฝ”๋“œ๋ž€ ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ์ฝ”๋“œ์˜ ์™„๋ฃŒ ์—ฌ๋ถ€์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ฆ‰์‹œ ๋‹ค์Œ ์ฝ”๋“œ๋กœ ๋„˜์–ด๊ฐ€๋Š” ์ฝ”๋“œ๋ฅผ ๋งํ•œ๋‹ค.
๋”ฐ๋ผ์„œ, ๋น„๋™๊ธฐ์ ์ธ ์ฝ”๋“œ๋Š” (์„ค๋ น ์ž‘์—…์„ ์™„๋ฃŒํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๋งŽ์ด ํ•„์š”ํ•˜๋”๋ผ๋„) ์š”์ฒญ๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ๋™์‹œ์— ์ผ์–ด๋‚˜๋Š” ๋™๊ธฐ์ ์ธ ์ฝ”๋“œ์™€ ๋‹ฌ๋ฆฌ, ์š”์ฒญ๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ๋™์‹œ์— ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.
๋ณ„๋„์˜ ์š”์ฒญ(fetch, XMLHttpRequest), ์‹คํ–‰ ๋Œ€๊ธฐ(addEventListener), ๋ณด๋ฅ˜(setTimeout) ๋“ฑ๊ณผ ๊ด€๋ จ๋œ ์ฝ”๋“œ๊ฐ€ ๋น„๋™๊ธฐ์ ์ธ ์ฝ”๋“œ์ด๋‹ค.

(2) ์ฝœ๋ฐฑ ์ง€์˜ฅ ํ•ด๊ฒฐ ๐Ÿ”ฅ

์ฝœ๋ฐฑ ์ง€์˜ฅ์„ ํ•ด๊ฒฐํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ์ต๋ช…์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ๊ธฐ๋ช… ํ•จ์ˆ˜๋กœ ์ „ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋น„๋™๊ธฐ ์ž‘์—…์„ ๋™๊ธฐ์ ์œผ๋กœ ํ˜น์€ ๋™๊ธฐ์ ์ธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ๋” ์ฒ˜๋ฆฌํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.
์ด๋ฅผ ์œ„ํ•ด ES6์—์„œ Promise์™€ Generator๊ฐ€ ๋„์ž…๋˜์—ˆ๊ณ , ES2017์—์„œ๋Š” async/await์ด ๋„์ž…๋˜์—ˆ๋‹ค.

[TIL] 211012 (promise / async) / [TIL] 211013 (await) ์ฐธ๊ณ 

Promise

new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•œ Promise์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜(์‹คํ–‰ ํ•จ์ˆ˜)๋Š” ํ˜ธ์ถœํ•  ๋•Œ ๋ฐ”๋กœ ์‹คํ–‰๋˜์ง€๋งŒ, ๊ทธ ๋‚ด๋ถ€์— resolve ๋˜๋Š” reject ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ตฌ๋ฌธ์ด ์žˆ์„ ๊ฒฝ์šฐ ๋‘˜ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „๊นŒ์ง€๋Š” ๋‹ค์Œ(then) ๋˜๋Š” ์˜ค๋ฅ˜ ๊ตฌ๋ฌธ(catch)๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋Š”๋‹ค.
๋”ฐ๋ผ์„œ, ๋น„๋™๊ธฐ ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ, ๋น„๋กœ์†Œ resolve ๋˜๋Š” reject๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋น„๋™๊ธฐ ์ž‘์—…์˜ ๋™๊ธฐ์  ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

Generator

๋น„๋™๊ธฐ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋Š” ์‹œ์ ๋งˆ๋‹ค next ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•จ์œผ๋กœ์จ Generator ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ์†Œ์Šค๊ฐ€ ์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

Promise + Async/await

var addCoffee = function (name) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      resolve(name);
    }, 500);
  });
};

var coffeeMaker = async function () {
  var coffeeList = "";
  var _addCoffee = async function (name) {
    coffeeList += (coffeeList ? "," : "") + await addCoffee(name);
  };
  await _addCoffee("์—์Šคํ”„๋ ˆ์†Œ");
  console.log(coffeeList);
  await _addCoffee("์•„๋ฉ”๋ฆฌ์นด๋…ธ");
  console.log(coffeeList);
  await _addCoffee("์นดํŽ˜๋ชจ์นด");
  console.log(coffeeList);
  await _addCoffee("์นดํŽ˜๋ผ๋–ผ");
  console.log(coffeeList);
};

coffeeMaker();

๋น„๋™๊ธฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” ํ•จ์ˆ˜ ์•ž์— async๋ฅผ ํ‘œํ˜„ํ•˜๊ณ , ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์‹ค์งˆ์ ์ธ ๋น„๋™๊ธฐ ์ž‘์—…์ด ํ•„์š”ํ•œ ์œ„์น˜๋งˆ๋‹ค await์„ ํ‘œ๊ธฐํ•œ๋‹ค.
Promise๊ฐ€ await์— ๋„˜๊ฒจ์ง€๋ฉด, await์€ Promise๊ฐ€ fulfill ๋˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€, ํ•ด๋‹น ๊ฐ’์„ return ํ•œ๋‹ค.
์ด๋ฅผ ํ†ตํ•ด promise์˜ then๊ณผ ํก์‚ฌํ•œ ํšจ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

coffeeMaker()
await _addCoffee("์—์Šคํ”„๋ ˆ์†Œ")
await addCoffee("์—์Šคํ”„๋ ˆ์†Œ")
0.5์ดˆ ๋’ค์— "์—์Šคํ”„๋ ˆ์†Œ" ๊ฐ’์„ ๋‹ด์€ resolve ํ•จ์ˆ˜ ํ˜ธ์ถœ ( = promise ์ƒํƒœ fullfilled)
_addCoffee ํ•จ์ˆ˜์˜ await addCoffee("์—์Šคํ”„๋ ˆ์†Œ")๊ฐ€ "์—์Šคํ”„๋ ˆ์†Œ"๋ฅผ return


โœจ ๋‚ด์ผ ํ•  ๊ฒƒ

  1. ํด๋กœ์ €
profile
๋Šฅ๋™์ ์œผ๋กœ ์‚ด์ž, ํ–‰๋ณตํ•˜๊ฒŒ๐Ÿ˜

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