Callbacks Concept

What is Callback?

  • JavaScript์—์„œ ํ•จ์ˆ˜(function)์€ ์ผ๊ธ‰ ๊ฐ์ฒด์ž„.

    • ์ฆ‰, ํ•จ์ˆ˜๋Š” object type์ด๋ฉฐ ๋‹ค๋ฅธ ์ผ๊ธ‰ ๊ฐ์ฒด์™€ ๋˜‘๊ฐ™์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Œ

  • function ์ž์ฒด๊ฐ€ object์ด๋ฏ€๋กœ

    • ๋ณ€์ˆ˜์•ˆ์— ๋‹ด์„ ์ˆ˜๋„ ์žˆ๊ณ ,

    • ์ธ์ˆ˜๋กœ์„œ ๋‹ค๋ฅธ ํ•จ์ˆ˜์— ์ „๋‹ฌ ํ•ด ์ค„์ˆ˜๋„ ์žˆ๊ณ ,

    • ํ•จ์ˆ˜์—์„œ ๋งŒ๋“ค์–ด์งˆ ์ˆ˜๋„ ์žˆ๊ณ ,

    • ๋ฐ˜ํ™˜ ๋  ์ˆ˜๋„ ์žˆ์Œ

Callback

: an asynchronous equivalent for a function

Callback Function

: ํŠน์ • ํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ์„œ ์ „๋‹ฌ๋œ ํ•จ์ˆ˜

-> Callback function์€ ๊ทธ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ๋ฐ›์€ ํ•จ์ˆ˜์•ˆ์—์„œ ํ˜ธ์ถœ๋˜๊ฒŒ ๋œ๋‹ค!

ex)

$("#btn_1").click(function() {
    alert("Btn 1 Clicked!")
});
  • click method์˜ ์ธ์ˆ˜๊ฐ€ ๋ณ€์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜์ž„

    • click method์˜ ์ธ์ˆ˜๊ฐ€ ๋ฐ”๋กœ Callback Function!

Node.js์—์„  ์ด๋Ÿฌํ•œ Callback ํ•จ์ˆ˜๊ฐ€ ๋งค์šฐ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค!

Blocking Code

  • Callback ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” Blocking code

    • ๋ง ๊ทธ๋Œ€๋กœ ์–ด๋–ค ์ž‘์—…์„ ์‹คํ–‰ํ•˜๊ณ  ๊ธฐ๋‹ค๋ฆฌ๋ฉด์„œ ์ฝ”๋“œ๊ฐ€ ๋ง‰ํžˆ๊ฒŒ ๋จ!

ex)

input.txt file

Let's understand what is a callback function.
What the HELL is it?

blocking.js

var fs = require("fs");

var data = fs.readFileSync('input.txt')

console.log(data.toString());
console.log("Program has ended!")

Result

$ node blocking.js
Let's understand what is a callback function.
What the HELL is it?
Program has ended!
  • text๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  program ์ข…๋ฃŒ message ์ถœ๋ ฅ๋จ

Non-Blocking Code

  • Callback ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋œ Non-Blocking Code

  • ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋  ๋•Œ, ํ”„๋กœ๊ทธ๋žจ์ด ํ•จ์ˆ˜๊ฐ€ ๋๋‚ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๊ทธ ์•„๋ž˜์— ์žˆ๋Š” code๋“ค์„ ์‹คํ–‰ํ•จ

    • ๊ทธ ๋‹ค์Œ ํ•จ์ˆ˜์— ์žˆ๋˜ ์ž‘์—…์ด ๋‹ค ๋๋‚˜๋ฉด callback ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•จ

ex)

input.txt๋Š” blocking code ์˜ˆ์‹œ์™€ ๊ฐ™์€ file ์‚ฌ์šฉ

non-blocking.js

var fs = require("fs");

fs.readFile('input.txt', function (err, data) {
    if (err) return console.error(err);
    console.log(data.toString());
});

console.log("Program has ended");
  • ๋ชจ๋“  Node application์˜ ๋น„๋™๊ธฐ์‹ ํ•จ์ˆ˜์—์„œ๋Š” ์ฒซ๋ฒˆ์งธ parameter๋กœ๋Š” error๋ฅผ, ๋งˆ์ง€๋ง‰ parameter๋กœ๋Š” callback ํ•จ์ˆ˜๋ฅผ ๋ฐ›์Œ

  • fs.readFile() ํ•จ์ˆ˜๋Š” ๋น„๋™๊ธฐ์‹์œผ๋กœ ํŒŒ์ผ์„ ์ฝ๋Š” ํ•จ์ˆ˜

    • ๋„์ค‘์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด err ๊ฐ์ฒด์— error ๋‚ด์šฉ์„ ๋‹ด๊ณ ,

    • ๊ทธ๋ ‡์ง€ ์•Š์„์‹œ์—๋Š” ํŒŒ์ผ ๋‚ด์šฉ์„ ๋‹ค ์ฝ๊ณ  ์ถœ๋ ฅํ•จ!

Result

$ node non-blocking.js 
Program has ended
Let's understand what is a callback function.
What the HELL is it?
  • readFile() method๊ฐ€ ์‹คํ–‰ ๋œ ํ›„, program์ด method๊ฐ€ ๋๋‚ ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•˜์ง€ ์•Š๊ณ  ๊ณง๋ฐ”๋กœ ๋‹ค์Œ ๋ช…๋ น์–ด๋กœ ์ง„ํ–‰ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์—, program์ด ๋๋‚ฌ๋‹ค๋Š” message๋ฅผ ์ถœ๋ ฅ ํ•œ ํ›„์—, text ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•จ

    • ๊ทธ๋ ‡๋‹ค๊ณ  program์ด ๋๋‚˜๊ณ  ๋‚˜์„œ text ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•œ ๊ฒƒ์€ ์•„๋‹˜!

    • program์ด ์‹ค์งˆ์ ์œผ๋กœ ๋๋‚œ๊ฑด text๊ฐ€ ์ถœ๋ ฅ๋œ ํ›„ ์ด๋‹ค!

      • ๋งŒ์•ฝ์— readFile() ๋‹ค์Œ์— ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ๊ทธ๋ƒฅ console.log()๊ฐ€ ์•„๋‹ˆ๋ผ readFile() ๋ณด๋‹ค ์ž‘์—…์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ฆฌ๋Š” ์ฝ”๋“œ์˜€๋‹ค๋ฉด text๋ฅผ ๋จผ์ € ์ถœ๋ ฅํ•˜๊ฒŒ ๋  ๊ฒƒ!

Wrap-up

Callback ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ ‡๊ฒŒ program์˜ ํ๋ฆ„์„ ๋Š์ง€ ์•Š์Œ์œผ๋กœ์จ, non-blocking code๋ฅผ ์‚ฌ์šฉํ•˜๋Š” server๋Š” blocking code๋ฅผ ์‚ฌ์šฉํ•˜๋Š” server ๋ณด๋‹ค ๋” ๋งŽ์€ ์–‘์˜ ์š”์ฒญ์„ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค!

Last updated