Chapter 4: Callback Function


Core JavaScript(์ฝ”์–ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ) ๋‚ด์šฉ ์ •๋ฆฌ


01. callback function

  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜: ๋‹ค๋ฅธ ์ฝ”๋“œ์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ํ•จ์ˆ˜ + ๊ทธ ์ œ์–ด๊ถŒ๋„ ํ•จ๊ป˜ ์œ„์ž„ํ•œ ํ•จ์ˆ˜
  • callback: ๋˜๋Œ์•„ ํ˜ธ์ถœํ•ด๋‹ฌ๋ผ
    โ‡’ ์–ด๋–ค ํ•จ์ˆ˜ X๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ โ€˜ํŠน์ • ์กฐ๊ฑด์ผ ๋•Œ ํ•จ์ˆ˜ Y๋ฅผ ์‹คํ–‰ํ•ด์„œ ๋‚˜์—๊ฒŒ ์•Œ๋ ค๋‹ฌ๋ผโ€™๋Š” ์š”์ฒญ์„ ํ•จ๊ป˜ ๋ณด๋ƒ„
    โ‡’ ์ด ์š”์ฒญ์„ ๋ฐ›์€ ํ•จ์ˆ˜ X๋Š” ํ•ด๋‹น ์กฐ๊ฑด์ด ๊ฐ–์ถฐ์กŒ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์Šค์Šค๋กœ ํŒ๋‹จํ•˜๊ณ  Y๋ฅผ ์ง์ ‘ ํ˜ธ์ถœ


02. ์ œ์–ด๊ถŒ

1. ํ˜ธ์ถœ ์‹œ์ 

  • setInterval์„ ์‹คํ–‰ํ•˜๋ฉด ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ๋‚ด์šฉ ์ž์ฒด๋ฅผ ํŠน์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ ์œ ํ•œ ID ๊ฐ’์ด ๋ฐ˜ํ™˜๋จ

    ์ด๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด๋Š” ์ด์œ ๋Š” ๋ฐ˜๋ณต ์‹คํ–‰๋˜๋Š” ์ค‘๊ฐ„์— ์ข…๋ฃŒ(clearInterval)ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ž„

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

  • timer ๋ณ€์ˆ˜์—๋Š” setInterval์˜ ID ๊ฐ’์ด ๋‹ด๊น€

    setInterval์— ์ „๋‹ฌํ•œ ์ฒซ ๋ฒˆ์งธ ์ธ์ž์ธ cbFunc ํ•จ์ˆ˜(์ฝœ๋ฐฑ ํ•จ์ˆ˜)๋Š” 0.3์ดˆ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ์‹คํ–‰๋  ๊ฒƒ

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

  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ๋ฐ›์€ ์ฝ”๋“œ(setInterval)๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์ ์— ๋Œ€ํ•œ ์ œ์–ด๊ถŒ์„ ๊ฐ€์ง

    Code                       | ํ˜ธ์ถœ ์ฃผ์ฒด     | ์ œ์–ด๊ถŒ
    -------------------------------------------------------
    cbFunc();                  | ์‚ฌ์šฉ์ž       | ์‚ฌ์šฉ์ž
    -------------------------------------------------------
    setInterval(cbFunc, 300);  | setInterval | setInterval

2. ์ธ์ž

  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ๋ฐ›์€ ์ฝ”๋“œ(map)์€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ธ์ž์— ์–ด๋–ค ๊ฐ’๋“ค์„ ์–ด๋–ค ์ˆœ์„œ๋กœ ๋„˜๊ธธ ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ์ œ์–ด๊ถŒ์„ ๊ฐ€์ง

    // Array.prototype.map(callback[, thisArg])
    // callback: function(currentValue, index, array)
    
    var newArr = [10, 20, 30].map(function(currentValue, index){
        console.log(currentValue, index);
        return currentValue + 5;
    });
    
    console.log(newArr)
    >>>
    10 0
    20 1
    30 2
    [15, 25, 35]


03. ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜๋‹ค

  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ์–ด๋–ค ๊ฐ์ฒด์˜ method๋ฅผ ์ „๋‹ฌํ•˜๋”๋ผ๋„ ๊ทธ method๋Š” method๊ฐ€ ์•„๋‹Œ function์œผ๋กœ์„œ ํ˜ธ์ถœ๋จ

  • forEach์— obj๋ฅผ this๋กœ ํ•˜๋Š” method(logValues)๋ฅผ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, obj.logValues๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ํ•จ์ˆ˜๋งŒ ์ „๋‹ฌํ•œ ๊ฒƒ

    ์ด ํ•จ์ˆ˜๋Š” method๋กœ์„œ ํ˜ธ์ถœํ•  ๋•Œ๊ฐ€ ์•„๋‹Œ ํ•œ, obj์™€์˜ ์ง์ ‘์ ์ธ ์—ฐ๊ด€์ด ์—†์–ด์ง

    forEach์— ์˜ํ•ด ์ฝœ๋ฐฑ์ด ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ๋˜๊ณ , ๋ณ„๋„๋กœ this๋ฅผ ์ง€์ •ํ•˜๋Š” ์ธ์ž๋ฅผ ๋„˜๊ธฐ์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this๋Š” ์ „์—ญ๊ฐ์ฒด

    var obj = {
        logValues: function(v, i){
            console.log(this, v, i)
        }
    };
    
    obj.logValues(1, 2);
    >>>
    {logValues: f} 1 2
    
    [1, 2, 3].forEach(obj.logValues);
    >>>
    Window{...} 1 0
    Window{...} 2 1
    Window{...} 3 2


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

  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๊ฐ€ ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ๋ฐ”๋ผ๋ณด๊ฒŒ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?

    โ‡’ ์ „ํ†ต์ ์œผ๋กœ๋Š” this๋ฅผ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ๋‹ด์•„ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ํ™œ์šฉํ•  ํ•จ์ˆ˜์—์„œ๋Š” this ๋Œ€์‹  ๊ทธ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•˜๊ณ , ์ด๋ฅผ ํด๋กœ์ €๋กœ ๋งŒ๋“œ๋Š” ๋ฐฉ์‹

    ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ์‹์€ ์‹ค์ œ๋กœ this๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„๋ฟ๋”๋Ÿฌ ๋ฒˆ๊ฑฐ๋กญ๋‹ค.

    var obj = {
        name: 'obj1',
        func: function(){
            var self = this;
            return function(){
                console.log(self.name);
            };
        }
    };
    
    var callback = obj.func();
    
    setTimeout(callback, 1000);
    >>> obj1  // 1์ดˆ(1000ms) ๋’ค ์‹คํ–‰

  • this๋ฅผ ์•„์˜ˆ ์•ˆ ์“ฐ๋Š” ๋ฐฉ๋ฒ•

    ๊ทธ๋Ÿฌ๋‚˜ this๋ฅผ ์ด์šฉํ•ด ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์— ์žฌํ™œ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
    (์ฒ˜์Œ๋ถ€ํ„ฐ ๋ฐ”๋ผ๋ณผ ๊ฐ์ฒด๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ obj๋กœ ์ง€์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ๋ฐฉ๋ฒ•์œผ๋กœ๋„ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋ฐ”๋ผ๋ณด๊ฒŒ๋” ํ•  ์ˆ˜ ์—†๋‹ค.)

    var obj = {
        name: 'obj1',
        func: function(){
            console.log(obj.name);
        }
    };
    
    setTimeout(obj.func, 1000);
    >>> obj1  // 1์ดˆ(1000ms) ๋’ค ์‹คํ–‰

  • ES5์—์„œ ๋“ฑ์žฅํ•œ bind method ์‚ฌ์šฉ (3์žฅ ์ฐธ์กฐ)

    var obj1 = {
        name: 'obj1',
        func: function(){
            console.log(this.name);
        }
    };
    
    setTimeout(obj1.func.bind(obj1), 1000);
    >>> obj1  // 1์ดˆ(1000ms) ๋’ค ์‹คํ–‰
    
    var obj2 = {name: 'obj2'};
    setTimeout(obj1.func.bind(obj2), 1500);
    >>> obj2  // 1.5์ดˆ(1500ms) ๋’ค ์‹คํ–‰


05. Callback hell๊ณผ ๋น„๋™๊ธฐ ์ œ์–ด

  • ์ฝœ๋ฐฑ ์ง€์˜ฅ(callback hell):
    ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ต๋ช… ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ณผ์ •์ด ๋ฐ˜๋ณต๋˜์–ด ์ฝ”๋“œ์˜ ๋“ค์—ฌ์“ฐ๊ธฐ ์ˆ˜์ค€์ด ๊ฐ๋‹นํ•˜๊ธฐ ํž˜๋“ค ์ •๋„๋กœ ๊นŠ์–ด์ง€๋Š” ํ˜„์ƒ

    ์ฃผ๋กœ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋‚˜ ์„œ๋ฒ„ ํ†ต์‹ ๊ณผ ๊ฐ™์ด ๋น„๋™๊ธฐ์ ์ธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์ด๋Ÿฐ ํ˜•ํƒœ๊ฐ€ ์ž์ฃผ ๋“ฑ์žฅ

1. Synchronous(๋™๊ธฐ)

  • ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ์ฝ”๋“œ๊ฐ€ ์™„๋ฃŒ๋œ ํ›„์—์•ผ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ์‹
  • CPU์˜ ๊ณ„์‚ฐ์— ์˜ํ•ด ์ฆ‰์‹œ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋Œ€๋ถ€๋ถ„์˜ ์ฝ”๋“œ
    ๊ณ„์‚ฐ์‹์ด ๋ณต์žกํ•ด์„œ CPU๊ฐ€ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์‹œ๊ฐ„์ด ๋งŽ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋ผ ํ•˜๋”๋ผ๋„ ๋™๊ธฐ์ ์ธ ์ฝ”๋“œ

2. Asynchronous(๋น„๋™๊ธฐ)

  • ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ์ฝ”๋“œ์˜ ์™„๋ฃŒ ์—ฌ๋ถ€์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ฆ‰์‹œ ๋‹ค์Œ ์ฝ”๋“œ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๋ฐฉ์‹
  • ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์— ์˜ํ•ด ํŠน์ • ์‹œ๊ฐ„์ด ๊ฒฝ๊ณผ๋˜๊ธฐ ์ „๊นŒ์ง€ ์–ด๋–ค ํ•จ์ˆ˜์˜ ์‹คํ–‰์„ ๋ณด๋ฅ˜ํ•œ๋‹ค๊ฑฐ๋‚˜(setTimeout),
    ์‚ฌ์šฉ์ž์˜ ์ง์ ‘์ ์ธ ๊ฐœ์ž…์ด ์žˆ์„ ๋•Œ ๋น„๋กœ์†Œ ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ๋Œ€๊ธฐํ•œ๋‹ค๊ฑฐ๋‚˜(addEventListener),
    ์›น๋ธŒ๋ผ์šฐ์ € ์ž์ฒด๊ฐ€ ์•„๋‹Œ ๋ณ„๋„์˜ ๋Œ€์ƒ์— ๋ฌด์–ธ๊ฐ€๋ฅผ ์š”์ฒญํ•˜๊ณ  ์‘๋‹ต์ด ์™”์„ ๋•Œ ๋น„๋กœ์†Œ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ๋Œ€๊ธฐํ•˜๋Š”(XMLHttpRequest) ๋“ฑ,
    ๋ณ„๋„์˜ ์š”์ฒญ, ์‹คํ–‰ ๋Œ€๊ธฐ, ๋ณด๋ฅ˜ ๋“ฑ๊ณผ ๊ด€๋ จ๋œ ์ฝ”๋“œ๋Š” ๋น„๋™๊ธฐ์ ์ธ ์ฝ”๋“œ
  • ํ˜„๋Œ€์˜ JS๋Š” ์›น์˜ ๋ณต์žก๋„๊ฐ€ ๋†’์•„์ง„ ๋งŒํผ ๋น„๋™๊ธฐ์ ์ธ ์ฝ”๋“œ์˜ ๋น„์ค‘์ด ์˜ˆ์ „๋ณด๋‹ค ํ›จ์”ฌ ๋†’์•„์ง โ‡’ ์ฝœ๋ฐฑ ์ง€์˜ฅ์— ๋น ์ง€๊ธฐ ์‰ฌ์›€

  • Callback hell ์˜ˆ์‹œ

    ๋“ค์—ฌ์“ฐ๊ธฐ ์ˆ˜์ค€์ด ๊ณผ๋„ํ•˜๊ฒŒ ๊นŠ์Œ + ๊ฐ’์ด ์ „๋‹ฌ๋˜๋Š” ์ˆœ์„œ๊ฐ€ โ€˜์•„๋ž˜์—์„œ ์œ„๋กœโ€™ ํ–ฅํ•˜๊ณ  ์žˆ์–ด ์–ด์ƒ‰

    setTimeout(function(name){
        var coffeeList = name;
        console.log(coffeeList);
    
        setTimeout(function(name){
            coffeeList += ', ' + name;
            console.log(coffeeList);
        }, 500, 'Latte');
    }, 500, 'Americano');
    
    >>>
    Americano
    Americano, Latte

  • Callback hell ํ•ด๊ฒฐ (์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ๋ชจ๋‘ ๊ธฐ๋ช…ํ•จ์ˆ˜๋กœ ์ „ํ™˜)

    ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ผ๋ฟ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜ ์„ ์–ธ๊ณผ ํ•จ์ˆ˜ ํ˜ธ์ถœ๋งŒ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ ์ˆœ์„œ๋Œ€๋กœ ์ฝ์–ด๋‚ด๋ ค๊ฐ€๋Š” ๋ฐ ์–ด๋ ค์›€์ด ์—†์Œ

    ๋ณ€์ˆ˜๋ฅผ ์ตœ์ƒ๋‹จ์œผ๋กœ ๋Œ์–ด์˜ฌ๋ฆผ์œผ๋กœ์จ ์™ธ๋ถ€์— ๋…ธ์ถœํ•˜๊ฒŒ ๋์ง€๋งŒ ์ „์ฒด๋ฅผ ์ฆ‰์‹œ ์‹คํ–‰ ํ•จ์ˆ˜ ๋“ฑ์œผ๋กœ ๊ฐ์‹ธ๋ฉด ํ•ด๊ฒฐ ๊ฐ€๋Šฅ

    But, ์ผํšŒ์„ฑ ํ•จ์ˆ˜๋ฅผ ์ „๋ถ€ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋ฉด ์ฝ”๋“œ๋ช…์„ ์ผ์ผ์ด ๋”ฐ๋ผ๋‹ค๋…€์•ผ ํ•˜๋ฏ€๋กœ ํ—ท๊ฐˆ๋ฆด ์†Œ์ง€๊ฐ€ ์žˆ์Œ

    var coffeeList = '';
    
    var addAmericano = function(name){
        coffeeList = name;
        console.log(coffeeList);
        setTimeout(addLatte, 500, 'Latte');
    };
    
    var addLatte = function(name){
        coffeeList += ', ' + name;
        console.log(coffeeList);
    };
    
    setTimeout(addAmericano, 500, 'Americano')
    
    >>>
    Americano
    Americano, Latte

3. ๋น„๋™๊ธฐ ์ž‘์—…์˜ ๋™๊ธฐ์  ํ‘œํ˜„

1) ES6์˜ Promise

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

    new Promise(function(resolve){
        setTimeout(function(){
            var name = 'Americano';
            console.log(name);
            resolve(name);
        }, 500);
    }).then(function(prevName){
        return new Promise(function(resolve){
            setTimeout(function(){
                var name = prevName + ', Latte';
                console.log(name);
                resolve(name);
            }, 500);
        });
    });
    
    >>>
    Americano
    Americano, Latte
    // ์œ„์˜ ๋ฐ˜๋ณต์ ์ธ ๋‚ด์šฉ์„ ํ•จ์ˆ˜ํ™” (ํด๋กœ์ €๋Š” 5์žฅ ์ฐธ์กฐ)
    
    var addCoffee = function(name){
        return function(prevName){
            return new Promise(function(resolve){
                setTimeout(function(){
                    var newName = prevName ? (prevName + ', ' + name) : name;
                    console.log(newName);
                    resolve(newName);
                }, 500);
            });
        };
    };
    
    addCoffee('Americano')()
        .then(addCoffee('Latte'));
    >>>
    Americano
    Americano, Latte

2) ES6์˜ Generator

  • *์ด ๋ถ™์€ ํ•จ์ˆ˜๊ฐ€ Generator ํ•จ์ˆ˜
  • Generator ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด Iterator๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š”๋ฐ, Iterator๋Š” next๋ผ๋Š” method๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
    โ‡’ next method๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด Generator ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ๊ฐ€์žฅ ๋จผ์ € ๋“ฑ์žฅํ•˜๋Š” yield์—์„œ ํ•จ์ˆ˜ ์‹คํ–‰์„ ๋ฉˆ์ถค
    โ‡’ ์ดํ›„ ๋‹ค์‹œ next method๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์•ž์„œ ๋ฉˆ์ท„๋˜ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๊ทธ ๋‹ค์Œ์— ๋“ฑ์žฅํ•˜๋Š” yield์—์„œ ํ•จ์ˆ˜ ์‹คํ–‰์„ ๋ฉˆ์ถค
  • ์ฆ‰, ๋น„๋™๊ธฐ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋Š” ์‹œ์ ๋งˆ๋‹ค next method๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด, Generator ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ์†Œ์ˆ˜๊ฐ€ ์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰๋จ

    var addCoffee = function(prevName, name){
        setTimeout(function(){
            coffeeMaker.next(prevName ? prevName + ', ' + name : name);
        }, 500);
    };
    
    var coffeeGenerator = function*(){
        var americano = yield addCoffee('', 'Americano');
        console.log(americano);
        var latte = yield addCoffee(americano, 'Latte');
        console.log(latte);
    };
    
    var coffeeMaker = coffeeGenerator();
    coffeeMaker.next();
    
    >>>
    Americano
    Americano, Latte

3) ES2017์˜ async/await

  • ๊ฐ€๋…์„ฑ์ด ๋›ฐ์–ด๋‚˜๋ฉด์„œ ์ž‘์„ฑ๋ฒ•๋„ ๊ฐ„๋‹จ
  • ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” ํ•จ์ˆ˜ ์•ž์— async๋ฅผ ํ‘œ๊ธฐํ•˜๊ณ , ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์‹ค์ง์ ์ธ ๋น„๋™๊ธฐ ์ž‘์—…์ด ํ•„์š”ํ•œ ์œ„์น˜๋งˆ๋‹ค await๋ฅผ ํ‘œ๊ธฐ
    โ‡’ ๋’ค์˜ ๋‚ด์šฉ์„ Promise๋กœ ์ž๋™ ์ „ํ™˜ํ•˜๊ณ , ํ•ด๋‹น ๋‚ด์šฉ์ด resolve๋œ ์ดํ›„์—์•ผ ๋‹ค์Œ์œผ๋กœ ์ง„ํ–‰ (Promise์˜ then๊ณผ ํก์‚ฌํ•œ ํšจ๊ณผ)

    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('Americano');
        console.log(coffeeList);
        await _addCoffee('Latte');
        console.log(coffeeList);
    };
    
    coffeeMaker();
    >>>
    Americano
    Americano, Latte


Reference

Core JavaScript(์ฝ”์–ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ)