CALLBACK

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์ด ์•„๋‹ˆ๋ผ ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์‹œ์Šคํ…œ์— ์˜ํ•ด ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์ด๋‹ค.

var button = document.getElementById('myButton');
button.addEventListener('click',function(){
  console.log('button clicked!');
});

JavaScript์—์„œ ํ•จ์ˆ˜๋Š” First-class object์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ํ•จ์ˆ˜๋Š” ๋ณ€์ˆ˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

First-class object(์ผ๊ธ‰ ๊ฐ์ฒด)๋ž€ ์ƒ์„ฑ, ๋Œ€์ž…, ์—ฐ์‚ฐ, ์ธ์ž ๋˜๋Š” ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ์„œ์˜ ์ „๋‹ฌ ๋“ฑ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ๊ธฐ๋ณธ์  ์กฐ์ž‘์„ ์ œํ•œ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€์ƒ์„ ์˜๋ฏธํ•œ๋‹ค.

  1. ๋ฌด๋ช…์˜ literal(๋ฆฌํ„ฐ๋Ÿด)๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅ

  2. ๋ณ€์ˆ˜๋‚˜ ์ž๋ฃŒ ๊ตฌ์กฐ(๊ฐ์ฒด, ๋ฐฐ์—ด ๋“ฑ)์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

  3. ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

  4. ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

// 1, 2
var increase = function(num){
    return ++num;
}
var decrease = function(num){
    return --num;
}
var predicates = {increase, decrease};
// 3,4
function makeCounter(predicate){
    var num = 0;
    return function(){
        num = predicate(num);
        return num;
    }
}

var increaser = makeCounter(predicates.increase);
console.log(increaser());
//=> 1
console.log(increaser());
//=> 2

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋˜๊ณ  ์ „๋‹ฌ๋ฐ›์€ ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€์—์„œ ์–ด๋Š ํŠน์ • ์‹œ์ ์— ์‹คํ–‰๋œ๋‹ค.

setTimeout(function(){
    console.log('1์ดˆํ›„ ์ถœ๋ ฅ');
},1000);

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ์ฃผ๋กœ ๋น„๋™๊ธฐ์‹ ์ฒ˜๋ฆฌ ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉ๋œ๋‹ค. ์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” ์ฝœ๋ฐฑ Queue์— ๋“ค์–ด๊ฐ€ ์žˆ๋‹ค๊ฐ€ ํ•ด๋‹น ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํ˜ธ์ถœ๋œ๋‹ค.

function doSomething(){
    var name = 'crong';

    setTimeout(function(){
        console.log('My name is '+name);
    },3000);
}

doSomething();
//=> My name is crong

jQuery ajax

function getData(){
  var data;
  $.get('https://domain.com/products/1',function(response){
    data = response;
  });
  return data;
}

console.log(getData()); //=> undefined

์—ฌ๊ธฐ์„œ $.get ์ด ajax ํ†ต์‹ ์„ ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ํ•ด๋‹น url์— HTTP GET์š”์ฒญ์„ ํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค.

ํ•ด๋‹น ์„œ๋ฒ„๋กœ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋Š” response ์ธ์ž์— ๋‹ด๊ธด๋‹ค.

ํ•˜์ง€๋งŒ getData() ๋ฅผ ํ˜ธ์ถœ ํ–ˆ์„๋•Œ ๊ฒฐ๊ณผ๊ฐ€ undefined์ด๋‹ค. ์™œ ๊ทธ๋Ÿด๊นŒ?

$.get() ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญ ํ›„ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์˜ค๊ธฐ ์ „์— return data๋ฅผ์‹คํ–‰ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ด๋ ‡๊ฒŒ ํŠน์ • ๋กœ์ง์˜ ์‹คํ–‰์ด ๋๋‚  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์ฃผ์ง€ ์•Š๊ณ  ๋‚˜๋จธ์ง€ ์ฝ”๋“œ๋ฅผ ๋จผ์ € ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์ด๋‹ค.

์ด๋Ÿฌํ•œ ๋ฌธ์ œ์ ์€ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผํ• ๊นŒ?

Callback function

function getData(callbackFunc){
  $.get('https://domain.com/products/1',function(response){
    callbackFunc(response); // ์„œ๋ฒ„์—์„œ ๋ฐ›์€ ๋ฐ์ดํ„ฐ(response)๋ฅผ callbackFunc()ํ•จ์ˆ˜์— ๋„˜๊ฒจ์ค€๋‹ค.
  });
}

getData(function(data){
  console.log(data); //$.get()์˜ response ๊ฐ’์ด data์— ์ „๋‹ฌ๋œ๋‹ค.
});

Callback hell ์ฝœ๋ฐฑ์ง€์˜ฅ

์ฝœ๋ฐฑ ์ง€์˜ฅ์€ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋กœ์ง์„ ์œ„ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์—ฐ์†ํ•ด์„œ ์‚ฌ์šฉํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

$.get('url', function (response) {
    parseValue(response, function (id) {
        auth(id, function (result) {
            display(result, function (text) {
                console.log(text);
            });
        });
    });
});

์›น์„œ๋น„์Šค ๊ฐœ๋ฐœ์‹œ ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€ ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ๊นŒ์ง€ ์ธ์ฝ”๋”ฉ, ์‚ฌ์šฉ์ž ์ธ์ฆ ๋“ฑ๋“ฑ์„ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด ๋ชจ๋“  ๊ณผ์ •์„ ๋น„๋™๊ธฐ๋กœ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ์ฝœ๋ฐฑํ•จ์ˆ˜์•ˆ์— ์ฝœ๋ฐฑ์„ ํ˜ธ์ถœํ•˜๋Š” ํ˜•์‹(์ฝœ๋ฐฑ ์ค‘์ฒฉ)์˜ ๊ตฌ์กฐ๊ฐ€ ๋œ๋‹ค. ์ด์™€ ๊ฐ™์€ ์ฝ”๋“œ ๊ตฌ์กฐ๋ฅผ callback hell์ด๋ผ ํ•œ๋‹ค.

Callback hell์€ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋‚˜์˜๊ฒŒ ํ•˜์—ฌ ์‹ค์ˆ˜๋ฅผ ์œ ๋ฐœ์‹œํ‚ฌ ํ™•๋ฅ ์ด ๋†’์•„์ง€๋ฉฐ, error ์ฒ˜๋ฆฌ๊ฐ€ ๊ณค๋ž€ํ•˜๋‹ค.

Callback hell ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

function parseValueDone(id) {
    auth(id, authDone);
}
function authDone(result) {
    display(result, displayDone);
}
function displayDone(text) {
    console.log(text);
}
$.get('url', function (response) {
    parseValue(response, parseValueDone);
});

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ Promise ๋‚˜ Async ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋” ํŽธํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐธ์กฐํŽ˜์ด์ง€

Last updated