๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

JavaScript Basic

ํŽ˜์ด์ง€๐Ÿ“„๋ฅผ ๋™์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ JavaScript!! ๊ทธ ์‚ฌ์šฉ๋ฒ•์„ ๋‚ฑ๋‚ฑ์ด ํŒŒํ•ด์ณ๋ณด์ž!

Variableโ€‹

var

let / const

  1. ์„ ์–ธ๊ณผ ํ• ๋‹น

    • var

      var myVar; // ์„ ์–ธ
      myVar = 1; // ํ• ๋‹น
      var myVar = "Hello"; // ์žฌ์„ ์–ธ ๊ฐ€๋Šฅ
    • let

      let myLet; // ์„ ์–ธ
      myLet = 2; // ํ• ๋‹น
      myLet = 3; // ์žฌํ• ๋‹น ๊ฐ€๋Šฅ
      let myLet = "hi" // SyntaxError -> ์ด๋ฏธ ์„ ์–ธ๋จ
    • const

      const myConst // SyntaxError -> ์ดˆ๊ธฐํ™” ๋ˆ„๋ฝ
      const myConst = "hi"; // ๋ฐ˜๋“œ์‹œ ๊ฐ’๊ณผ ํ•จ๊ป˜ ์„ ์–ธ
      myConst = "bye" // TypeError -> ์žฌํ• ๋‹น ๋ถˆ๊ฐ€
  2. Scope

    • var

      let VarFunction = function () {
      var myVar = 0;
      if (true) {
      var myVar = 1;
      console.log(myVar); // 1
      }
      console.log(myVar); // 1
      };
    • let

      let LetFunction = function () {
      let myLet = 0;
      if (true) {
      let myLet = 1;
      console.log(myLet); // 1
      }
      console.log(myLet); // 0
      }

Hoistingโ€‹

  • var

    console.log(mooyeon); // undefined
    var mooyeon = "๋ฌด์—ฐ";

    ์œ„์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ดํ•ดํ•œ๋‹ค.

    var mooyeon; // hoisting
    console.log(mooyeon);
    mooyeon = "๋ฌด์—ฐ";
  • let

    console.log(dooly); // ReferenceError - ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์ „ ์ ‘๊ทผ ๊ธˆ์ง€
    let dooly = "๋‘˜๋ฆฌ";
  • ์ดํ•ดํ•˜๊ธฐ

    • var

      1. ์„ ์–ธ๊ณผ ๋™์‹œ์— ์ดˆ๊ธฐํ™”
      2. ํ• ๋‹น
    • let, const

      TDZ(temporal Dead Zone)์ด ์กด์žฌ

      1. ์„ ์–ธ
      2. TDZ
      3. ์ดˆ๊ธฐํ™”
      4. ํ• ๋‹น

Typeโ€‹

  • ์›์‹œํƒ€์ž… (primitive data type)
    • boolean
    • null
    • undefined
    • number
    • string
    • symbol (ES6+)
  • ๊ฐ์ฒดํƒ€์ž… (object)
    • object
  1. Number

    3 - 5;
    Infinity; // typeof Infinity >> "number"
    NaN; // typeof NaN >> "number" ์‚ฐ์ˆ  ์—ฐ์‚ฐ ๋ถˆ๊ฐ€, number๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” number..
    10 / 0; // Infinity
    0 / 0; // NaN
  2. String

    let myName = "๋ฌด์—ฐ";
    myName = '๋ฌด์—ฐ';
    // ` (backtick) : ES6+, ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด
    // string interpolation, ์ค„๋ฐ”๊ฟˆ(๊ฐœํ–‰) - ์—”ํ„ฐ๋„ ์ธ์‹ํ•˜์—ฌ ์ถœ๋ ฅ๋œ๋‹ค.
    myName = `๋ฌด
    ์—ฐ`;
  3. Boolean

    true;
    false;
  4. Empty value

    undefined; // typeof undefined >> "undefined"
    null; // typeof null >> "object"

Operatorโ€‹

  1. ๋™๋“ฑ ์—ฐ์‚ฐ์ž (==)

    1 == '1'; // true
  2. ์ผ์น˜ ์—ฐ์‚ฐ์ž (===)

    1 === '1' // false
  3. ํ• ๋‹น

    • =, +=, -=, *=, /=, ...
  4. ๋น„๊ต

    • >, <, >=, <=, ...
  5. ๋…ผ๋ฆฌ

    • and : &&
    • or : ||
  6. Not (!)

  7. ์‚ผํ•ญ์—ฐ์‚ฐ์ž ํ‘œํ˜„์‹

    ? true : false

    2 > 4 ? true : false

Flowโ€‹

  1. if๋ฌธ

    if (userName === "ssafy") {
    message = "<h1>Hello ssafy</h1>";
    } else if (userName === "1q2w3e4r") {
    message = "<h1>Admin page์ž…๋‹ˆ๋‹ค</h1>";
    } else {
    message = `<h1>${userName} ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.</h1>`;
    }
  2. switch๋ฌธ

    switch (userName) {
    case "1q2w3e4r": {
    message = "<h2>๊ด€๋ฆฌ์ž๋‹˜ ์ถฉ์„ฑ์ถฉ์„ฑ</h2>";
    break;
    }
    case "ssafy": {
    message = '<a href="http://edu.ssafy.com">์‹ธํ”ผ</a>';
    break;
    }
    default: {
    message = `<h1>${userName} ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.</h1>`;
    }
    }
  3. for๋ฌธ

    /*
    *๋ฐ˜๋ณต๋ฌธ
    var๋Š” ํ•จ์ˆ˜์Šค์ฝ”ํ”„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์„œ for๋ฌธ ์ดํ›„์— i ๋ณ€์ˆ˜์— ๊ฐ’์ด ์œ ์ง€
    let์€ ๋ธ”๋ก์Šค์ฝ”ํ”„๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์„œ for๋ฌธ ์ดํ›„์— ํ•ด๋‹น ๋ณ€์ˆ˜๋Š” ์—†์Œ.
    */
    for (var i = 0; i < 3; i++) {
    console.log(i);
    }
    console.log(i); // 3

    for (let i = 0; i < 3; i++) {
    console.log(i);
    }
    console.log(i); // ReferenceError
    let myArray = ["๋ฌด์—ฐ", "ํ˜„ํƒ", "์ˆœ๋ฒ”"];
    for (let name of myArray) {
    document.write(name);
    }

Functionโ€‹

  • ์„ ์–ธ์‹๊ณผ ํ‘œํ˜„์‹

    console.log(myAdd(2, 3)); // 5
    console.log(myAdd2(3, 5)); // ReferenceError: myAdd2 is not defined

    // ์„ ์–ธ์‹ -> hoisting ๋œ๋‹ค
    function myAdd(a, b) {
    return a + b;
    }

    // ํ‘œํ˜„์‹ -> hoisting ์•ˆ๋˜๋‹ˆ ์›ฌ๋งŒํ•˜๋ฉด ํ‘œํ˜„์‹์œผ๋กœ ํ•จ์ˆ˜ ๋งŒ๋“ค์ž.
    let myAdd2 = function (a, b) {
    return a + b;
    };
  • Arrow function

    let myFunction = function (a) {
    return a + 1;
    };
    // 1. function ํ‚ค์›Œ๋“œ ์‚ญ์ œ ํ›„ =>
    let myArrowFunction1 = a => {
    return a + 1;
    };
    // 1-1. ์ธ์ž๊ฐ€ ํ•˜๋‚˜๋ผ๋ฉด, ์†Œ๊ด„ํ˜ธ ์ƒ๋žต ๊ฐ€๋Šฅ
    // 1-2. ๋ฌธ์žฅ์ด ํ•œ ์ค„์ด๊ณ , ๋ฆฌํ„ด์ด๋ผ๋ฉด ์ค‘๊ด„ํ˜ธ ๋ฐ return ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ
    let myArrowFunction2 = (a) => a + 1;
    // 1-3. ์ธ์ž๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” () or _
    let ArrowGreeting2 = () => {
    console.log("happy!");
    };
  • ๊ธฐ๋ณธ ์ธ์ž

    let greeting2 = (name = "ํƒ€ํ‚ค") =>
    name(
    /*
    * ์ต๋ช…ํ•จ์ˆ˜
    */
    (function (a) {
    return a * 10;
    })(10)
    )((a) => a + 10)(10);

Arrayโ€‹

  1. Array ์„ ์–ธ

    let numbers = [10, 1, 3, 5];
  2. Index ์š”์†Œ ์ ‘๊ทผ

    numbers[0]; // 10
    numbers[-1]; // undefined
    numbers.length; // 4
  3. ๋’ค์ง‘๊ธฐ

    numbers.reverse(); // return + ์›๋ณธ ๋ณ€๊ฒฝ.
  4. ๋งˆ์ง€๋ง‰์— ์›์†Œ ์ถ”๊ฐ€

    numbers.push(20); // ๋งˆ์ง€๋ง‰์— ์›์†Œ ์ถ”๊ฐ€ + return (๊ธธ์ด)
  5. ๋งˆ์ง€๋ง‰ ์›์†Œ ์ œ๊ฑฐ ๋ฐ return

    numbers.pop(); // 20 : ๋งˆ์ง€๋ง‰ ์›์†Œ pop + return (ํ•ด๋‹น ์›์†Œ)
  6. ๊ฐ€์žฅ ์•ž์— ์›์†Œ ์ถ”๊ฐ€ ๋ฐ ๊ธธ์ด return

    numbers.unshift(3); // ๋งจ์•ž์— ์›์†Œ ์ถ”๊ฐ€ + return (๊ธธ์ด)
  7. ๋งจ์•ž ์›์†Œ ์ œ๊ฑฐ ๋ฐ return

    numbers.shift(); // ๋งจ์•ž ์›์†Œ ์ œ๊ฑฐ + return (ํ•ด๋‹น ์›์†Œ)
  8. ํฌํ•จ ์—ฌ๋ถ€ ํ™•์ธ

    numbers.includes(1); // true : ํฌํ•จ์—ฌ๋ถ€ ํ™•์ธ
  9. ํฌํ•จ ์—ฌ๋ถ€ + ๊ฐ€์žฅ ๋จผ์ € ์กด์žฌํ•˜๋Š” ์œ„์น˜ ๋ฐ˜ํ™˜

    numbers.indexOf(1); // ๊ฐ€์žฅ ๋จผ์ € ์กด์žฌํ•˜๋Š” ์œ„์น˜
  10. String์œผ๋กœ ๋ฐ”๊พธ๊ธฐ

    numbers.join(); // ๊ธฐ๋ณธ์ด ,
    numbers.join("-"); // -๋กœ ์—ฐ๊ฒฐ.

Objectโ€‹

  • Object ์ƒ์„ฑ

    // ํ‚ค๊ฐ’์„ ""๋กœ ๊ตณ์ด ์•ˆ๋ฌถ์–ด๋„ ๋œ๋‹ค.
    const me = {
    // ํ”„๋กœํผํ‹ฐ
    name: "kim",
    "phone number": "01012345678",
    phone: {
    type: "iphone XS MAX",
    },
    // ๋ฉ”์„œ๋“œ function ํ‚ค์›Œ๋“œ๋งŒ ์ž‘์„ฑํ•˜์ž!
    greeting: function () {
    console.log(this); // me
    console.log(`hi ${this.name}`); // this : '๋‚˜'๋ฅผ ๋œปํ•œ๋‹ค. self.name์ฒ˜๋Ÿผ
    },
    greeting2: () => {
    console.log(this); // ์ „์—ญ๊ฐ์ฒด window
    console.log(`hi ${this.name}`); // ์ด๊ฑด this๊ฐ€ ์•ˆ๋จนํžˆ๋„ค!? arrow function์—์„œ์˜ this๋Š” ๋ฌด์กฐ๊ฑด ์ƒ์œ„ object! ๋‚˜์ค‘๊ฐ€๋ฉด ์œ ์šฉํ• ๊ฑธ!?
    },
    };
    // ํ•จ์ˆ˜์„ ์–ธ์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์ต์ˆ™ํ•œ ํ˜•ํƒœ์˜ object ์ƒ์„ฑ ๊ฐ€๋Šฅ
    // ์ธ์ž๋ฅผ ๋ฐ›์•„์„œ ๋ญ”๊ฐ€๋ฅผ ํ•˜๋Š”, ๋‹ค์–‘ํ•œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ์„ ๋•Œ.
    // ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด ๋†“๊ณ  ''new'' ํ‚ค์›Œ๋“œ๋กœ ๋ถˆ๋Ÿฌ์™€ ๊ฐ์ฒด ์ƒ์„ฑ๊ฐ€๋Šฅ.
    const Person = function(name, phone) {
    this.name = name
    this.phone = phone
    this.greeting = function() {
    return 'hi' + this.name
    }
    }
    const moo = new Person('์ตœ๋ฌด์—ฐ', '010-????-????') // new! ํŠน์ •ํ•œ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ํ‚ค์›Œ๋“œ
    lee.name
    lee.greeting()

    // ๊ทธ๋Ÿผ arrow func ์œผ๋กœ๋„ ๋ ๊นŒ?
    const Animal = name => {
    this.name = name
    }
    const dog = new Animal('dog') // Error!!!!!!!!!
    // ์ƒ์„ฑ์ž ํ•จ์ˆ˜์—์„œ๋Š” arrow func ์‚ฌ์šฉ ๊ธˆ์ง€


    // object ๋ฆฌํ„ฐ๋Ÿด
    const name = '๊ฒจ๋ ˆ'
    const phone = '010-0000-2222'
    const greeting = function () {
    return 'hi,' + this.name
    }
    const you = {
    name,
    phone,
    greeting
    }
    //์ด๋Ÿฐ์‹์œผ๋กœ ์“ฐ๋Š”๊ฒŒ ์˜ค๋ธŒ์ ํŠธ ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ๋ฒ•. ์ถ•์•ฝํ˜•์ด๋‹ค ๋ผ๊ณ  ๊ฐ„๋‹จํžˆ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
  • ์˜ค๋ธŒ์ ํŠธ ๋ฆฌํ„ฐ๋Ÿด (ES6+)

    let book = "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์™„์ „ ์ •๋ณต";
    let album = {
    IU: ["์ข‹์€๋‚ ", "๋ฐคํŽธ์ง€"],
    BTS: ["์ž‘์€๊ฒƒ๋“ค์„ ์œ„ํ•œ ์‹œ"],
    };
    // ์œ„์˜ ๊ฐ’๋“ค์„ object์— ๋„ฃ์œผ๋ฉด ๋ฐ”๋กœ key,value๋กœ ์•Œ์•„์„œ ๋“ค์–ด๊ฐ„๋‹ค.
    let bag = {
    book,
    album,
    };
  • JSON

    // JSON (Javascript Object Notation - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์˜ค๋ธŒ์ ํŠธ ํ‘œ๊ธฐ๋ฒ•)
    // JSON์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์˜ค๋ธŒ์ ํŠธ ํ‘œ๊ธฐ๋ฒ•์„ ๊ฐ€์ง„ "๋ฌธ์ž์—ด"์ด๋‹ค
    // object -> JSON
    let jsonData = JSON.stringify(me);
    let myObject = JSON.parse(jsonDate);

Array helper methodโ€‹

  • Array.forEach

    let numbers = [1, 2, 3];

    // 1. ๋ฐ˜๋ณต๋ฌธ (for)
    for (let i = 0; i < numbers.length; i++) {
    console.log(numbers[i]);
    }

    // 2. ๋ฐ˜๋ณต๋ฌธ (for..of)
    for (let num of numbers) {
    console.log(num);
    }

    // 3. forEach
    numbers.forEach(function (num) {
    console.log(num);
    });
  • map

    ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ return ๊ฒฐ๊ณผ๋ฅผ ๊ฐ๊ฐ ์›์†Œ๋กœ ํ•˜๋Š” ๋ฐฐ์—ด์„ ๋ฆฌํ„ด

    let numbers = [1, 2, 3];
    let doubleNumbers = numbers.map(function (number) {
    return number * 2;
    });
    console.log(doubleNumbers);

    let numbers = [1, 2, 3];
    let doubleNumbers = numbers.map((number) => number * 2);
    console.log(doubleNumbers);
  • filter

    callback ํ•จ์ˆ˜์˜ return ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ธ ๊ฒƒ์„ ๊ฐ๊ฐ ์›์†Œ๋กœ ํ•˜๋Š” ๋ฐฐ์—ด์„ ๋ฆฌํ„ด

    // images์˜ ๋†’์ด๊ฐ€ 100๋ณด๋‹ค ์ž‘์€ object๋งŒ ๋‹ด์€ ๋ฐฐ์—ด
    // ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ
    let list = [];
    for (image of images) {
    if (image.height < 100) {
    list.push(image);
    }
    }
    console.log(list);
    // filter๋กœ
    list = images.filter(function (image) {
    return image.height < 100;
    });
    console.log(list);
  • reduce(callbackfn, initialvalue)

    return ๊ฒฐ๊ณผ๋ฅผ ๋ˆ„์ ํ•œ ๊ฒฐ๊ณผ๋ฅผ return

    let mySsafy = [100, 100, 95, 90];
    let total = 0;
    mySsafy.forEach(function (score) {
    total += score;
    });

    mySsafy.reduce(function (total, score) {
    // total: ๋ˆ„์ , score: ๊ฐ ์›์†Œ
    return total + score;
    }, 10000); // 10000 :์ดˆ๊ธฐ๊ฐ’, reduce์˜ ๋‘๋ฒˆ์งธ ์ธ์ž. ์—†์œผ๋ฉด ์ฒซ score๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ return

    mySsafy.reduce((total, score) => total + score, 10000);
  • find

    ์ผ์น˜ํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ์›์†Œ๋ฅผ return.

    mySsafy.find(function (score) {
    return score === 90;
    });
  • some, every

    // some: return์„ ๋งŒ์กฑํ•˜๋Š”๊ฒŒ ํ•˜๋‚˜๋ผ๋„ ์žˆ๋Š๋ƒ
    let myNumbers = [1, 2, 3, 4];
    myNumbers.some(function (number) {
    return number % 2 === 0;
    });
    // every: ๋ชจ๋“  ๊ฒƒ์ด return์„ ๋งŒ์กฑํ•˜๋Š๋ƒ
    myNumbers.every(function (number) {
    return number % 2 !== 0;
    });

DOMโ€‹

<body>
<h1 class="text">DOM ์กฐ์ž‘(Document Object Model)</h1>
<h2 class="text">์นดํŽ˜</h2>
<img src="my.png", alt="">
<ul id='my-list'>
<li>์•„๋ฉ”๋ฆฌ์นด๋…ธ</li>
<li>์ž๋ฐ”์นฉํ”„๋ผํ”„์น˜๋…ธ</li>
</ul>
</body>

Blockingโ€‹

  • setTimeout๊ณผ Callback

    function a() {
    console.log("a");
    }
    console.log("hi");
    setTimeout(a, 3000);
    console.log("bye"); // hi -> bye -> (3์ดˆ๋’ค) -> a

    function printHello() {
    console.log("Hello");
    }

    function baz() {
    console.log("baz"); // 1
    setTimeout(printHello, 3000); // 3- ๋น„๋™๊ธฐ๋กœ ๋™์ž‘ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. 3000์ด ์•„๋‹ˆ๋ผ 0์ด์–ด๋„ Hello๊ฐ€ ๋งˆ์ง€๋ง‰์— ์ถœ๋ ฅ๋œ๋‹ค..!!
    console.log("baz end"); //2
    }

    function bar() {
    console.log("bar");
    baz();
    }

    function foo() {
    console.log("foo");
    bar();
    }

    foo();

    function sum(x, callbackfn) {
    setTimeout(callbackfn, 1000, x + 1);
    }
    var result = 0;
    sum(2, function (x) {
    result = x;
    console.log(result); // 3
    });
    console.log(result); // 0

Event Loopโ€‹

  • Blocking๊ณผ ๊ฐ™์ด ์ž˜ ๋ณด์ž..
  • ์ด๋ฒคํŠธ ๋ฃจํ”„ : call stack, callback queue ํ™•์ธํ•˜๋Š” ์—ญํ• 
    • call stack์ด ๋น„์–ด์žˆ์œผ๋ฉด, callback queue์—์„œ call stack์œผ๋กœ ์ด๋™.
    • ์ด๋ฒคํŠธ(ํ•จ์ˆ˜์‹คํ–‰..)
    • tick(ํ‹ฑ)
  • synchronous / Asynchronous (๋™๊ธฐ / ๋น„๋™๊ธฐ)
    • Synchronous/Asynchronous๋Š” ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์˜ ์ž‘์—… ์™„๋ฃŒ ์—ฌ๋ถ€๋ฅผ ๋ˆ„๊ฐ€ ์‹ ๊ฒฝ์“ฐ๋ƒ๊ฐ€ ๊ด€์‹ฌ์‚ฌ๋‹ค.
    • ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์—๊ฒŒ callback์„ ์ „๋‹ฌํ•ด์„œ, ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์˜ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์ „๋‹ฌ๋ฐ›์€ callback์„ ์‹คํ–‰ํ•˜๊ณ , ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜๋Š” ์ž‘์—… ์™„๋ฃŒ ์—ฌ๋ถ€๋ฅผ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์œผ๋ฉด Asynchronous๋‹ค.
    • ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์˜ ์ž‘์—… ์™„๋ฃŒ ํ›„ ๋ฆฌํ„ด์„ ๊ธฐ๋‹ค๋ฆฌ๊ฑฐ๋‚˜, ๋˜๋Š” ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ ๋ฐ”๋กœ ๋ฆฌํ„ด ๋ฐ›๋”๋ผ๋„ ์ž‘์—… ์™„๋ฃŒ ์—ฌ๋ถ€๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜ ์Šค์Šค๋กœ ๊ณ„์† ํ™•์ธํ•˜๋ฉฐ ์‹ ๊ฒฝ์“ฐ๋ฉด Synchronous๋‹ค.
  • blocking / non-blocking
    • Blocking/NonBlocking์€ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๋ฐ”๋กœ ๋ฆฌํ„ดํ•˜๋Š๋ƒ ๋งˆ๋Š๋ƒ๊ฐ€ ๊ด€์‹ฌ์‚ฌ๋‹ค.
    • ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜๊ฐ€ ๋ฐ”๋กœ ๋ฆฌํ„ดํ•ด์„œ ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜์—๊ฒŒ ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ์ฃผ๊ณ , ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ์ผ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ๋ฅผ ์ค„ ์ˆ˜ ์žˆ์œผ๋ฉด NonBlocking์ด๋‹ค.
    • ๊ทธ๋ ‡์ง€ ์•Š๊ณ  ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜๊ฐ€ ์ž์‹ ์˜ ์ž‘์—…์„ ๋ชจ๋‘ ๋งˆ์น  ๋•Œ๊นŒ์ง€ ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜์—๊ฒŒ ์ œ์–ด๊ถŒ์„ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š๊ณ  ๋Œ€๊ธฐํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค๋ฉด Blocking์ด๋‹ค.

Axiosโ€‹

axios.get("https://jsonplaceholder.typicode.com/posts/1").then((response) => {
console.log(response);
document.write(`
<h1>${response.data.id} : ${response.data.title}</h1>
<p>${response.data.body}</p>
`);
return response.data;
});
console.log("bye");

Promiseโ€‹

๋ฐ์ดํ„ฐ๋ฅผ ์™ธ๋ถ€๋กœ ๋ถ€ํ„ฐ ๋ฐ›์•„์™€์„œ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž!

  1. ๋น„๋™๊ธฐ๊ฐ€ ์—†๋Š” ์ƒํƒœ์˜ ์ฝ”๋“œ

    function getData() {
    const data = { data: "some data" };
    return data;
    }

    let response = getData();
    console.log(response);
  2. setTimeout

    function getData() {
    let data;
    setTimeout(function () {
    console.log("์š”์ฒญ์„ ๋ณด๋ƒˆ์Šต๋‹ˆ๋‹ค.");
    data = { data: "some data" };
    }, 1000);
    return data;
    }
    let response1 = getData();
    console.log(response1); // undefined
    • data๋Š” ๊ทธ๋Ÿผ ์–ด๋””๋กœ ๊ฐ€๋Š”๊ฐ€!
  3. callback func ์ •์˜

    function getDataCallback(callback) {
    setTimeout(function () {
    console.log("์š”์ฒญ์„ ๋ณด๋ƒˆ์Šต๋‹ˆ๋‹ค");
    const data = { data: "some data" }; // ๋ฐ์ดํ„ฐ ๋„์ฐฉ! ํ•˜๋ฉด
    callback(data); // ์ด๋•Œ! ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ž‘์—…(data๋‹ด์•„์„œ ์ถœ๋ ฅ)์„ ์‹œ์ž‘!
    }, 1000);
    }
    // ํ•จ์ˆ˜ ํ˜ธ์ถœ. ์ธ์ž๋กœ 'ํ•จ์ˆ˜'๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š”๋ฐ, ๊ทธ๊ฒŒ ์ถœ๋ ฅํ•˜๋Š” ์ž‘์—…์„ ํ•˜๋Š” ํ•จ์ˆ˜. ์–˜~๋ฅผ ๋‚˜์ค‘์— callback ํ•˜๊ฒ ๋‹ค..!
    getDataCallback(function (data) {
    let response2 = data;
    console.log(response2);
    });
    • ๊ทธ๋Ÿฐ๋ฐ ์ด๊ฑด ์ฝœ๋ฐฑ์ง€์˜ฅ์„ ๋งŒ๋“ ๋‹ค...
  4. promise

    function getDataPromise() {
    // Promise ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ์ด๋Ÿฐ๊ฒŒ ์ƒ๊ฒผ๋‹ค
    return new Promise((resolve) => {
    setTimeout(function () {
    console.log("์š”์ฒญ์„ ๋ณด๋ƒˆ์Šต๋‹ˆ๋‹ค");
    const data = { data: "some data" };
    resolve(data);
    }, 1000);
    });
    }

    let response3 = getDataPromise();
    // ์ด๋Ÿฌ๋ฉด response3์— promise ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ๋ฆฌํ„ด๋œ๋‹ค๊ณ !
    console.log(response3); // 1. ์ถœ๋ ฅ์€ pending์ด๋ผ๊ณ  ๋œ๋‹ค. (์š”์ฒญ์ฒ˜๋ฆฌ์ค‘..?)
    console.log(response3); // 2. ์ด์ œ ์ถœ๋ ฅ์ด resolved๋ผ๊ณ  ๋œ๋‹ค. (resolveํ–ˆ์–ด, ํ•จ์ˆ˜ ํ˜ธ์ถœํ–ˆ์–ด ์ƒํƒœ)
    // ๊ทธ๋Ÿฌ๋ฉด resolved ๋ญ ์ด๋ ‡๊ฒŒ ๋‹ด๊ธฐ๋Š”๋ฐ ๊ทธ๊ฒƒ์„ ํ’€์–ด์ฃผ๋Š” ๊ฒƒ์ด .then
    response3.then((response) => console.log(response)); // 3. data ์ถœ๋ ฅ
    // ๊ทธ๋ž˜์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋‹ค.
    getDataPromise().then((response) => console.log(response));
    // ์ด๊ฒƒ ๋•๋ถ„์— .then .then .then ํ•˜๋ฉด์„œ ์ž‘์—…์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค
    • ์ฝœ๋ฐฑ์ง€์˜ฅ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ Promise!
  5. Promise ์ถ”๊ฐ€ ๋‚ด์šฉ

    function myPromise(url) {
    return new Promise((resolve, reject) => {
    // resolve ํ•˜๋‚˜๋งŒ ์•„๋‹ˆ๋ผ reject๋„ ๊ฐ™์ด ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ
    if (url === "http") {
    resolve("์„ฑ๊ณต");
    } else {
    reject("url์ด ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
    }
    });
    }
    const promise1 = myPromise("http");
    console.log(promise1);
    const promise2 = myPromise("www");
    console.log(promise2);
    • resolve๋กœ ๊ฐ€๋ฉด ๊ทธ ๋‹ค์Œ์œผ๋กœ .then์œผ๋กœ ๊ฐ€๊ณ 
    • reject๋กœ ๊ฐ€๋ฉด ๊ทธ ๋‹ค์Œ์œผ๋กœ .catch๋กœ ๊ฐ„๋‹ค. (์•„๋ฌด๋ฆฌ .then์ด ๋งŽ์•„๋„ creject๋กœ ๊ฐ€๋ฉด .then์€ ๋ฌด์‹œํ•œ๋‹ค๋Š” ๋ง)
    promise2
    .then((response) => {
    // ์ด ๋ถ€๋ถ„์€ ์‹คํ–‰์ด ์•ˆ๋œ๋‹ค๊ณ !
    console.log("์„ฑ๊ณต");
    console.log(response);
    })
    .catch((error) => {
    console.log("error");
    console.log(error);
    });
  6. chaining

    function getDataPromise() {
    // Promise ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ์ด๋Ÿฐ๊ฒŒ ์ƒ๊ฒผ๋‹ค
    return new Promise((resolve) => {
    setTimeout(function () {
    console.log("์š”์ฒญ์„ ๋ณด๋ƒˆ์Šต๋‹ˆ๋‹ค");
    const data = { data: "some data" };
    resolve(data);
    }, 1000);
    });
    }
    getDataPromise()
    .then((response) => {
    // response = data
    console.log(response); // {'data': 'some data'}
    return response.data; // 'some data'
    })
    .then((data) => {
    // data = 'some data'
    console.log(data); // 'some data'
    })
    .catch((error) => {
    console.log(error);
    });
  7. chaining 2

    • axios์—์„œ ์‚ฌ์šฉ
    axios
    .get("https://jsonplaceholder.typicode.com/posts/1")
    .then((response) => {
    return response.data.userId; // ์‹ค์ œ๋กœ๋Š” promise object๋กœ ํฌ์žฅ๋˜์–ด ๋ฆฌํ„ด
    })
    .then((userId) => {
    return axios.get(`https://jsonplaceholder.typicode.com/users/${userId}`);
    })
    .then((response) => {
    console.log(response);
    console.log(response.data.name);
    });
  8. async / await

    ๋™๊ธฐ ์ž‘์—…์ธ์ฒ™ ํ•˜๊ธฐ!!!๊ฐ€ ํ•ต์‹ฌ. + promise (promise๊ฐ€ ๋ฆฌํ„ด์ธ ํ•จ์ˆ˜๋ฅผ ๋Œ€์ƒ์œผ๋กœ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.)

    function getDataPromise() {
    // Promise ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ์ด๋Ÿฐ๊ฒŒ ์ƒ๊ฒผ๋‹ค
    return new Promise((resolve) => {
    setTimeout(function () {
    console.log("์š”์ฒญ์„ ๋ณด๋ƒˆ์Šต๋‹ˆ๋‹ค");
    const data = { data: "some data" };
    resolve(data);
    }, 1000);
    });
    }
    async function printData() {
    const response = await getDataPromise();
    console.log(response);
    }
    printData(); // {data: "some data"}
    • ์œ„์—์„œ jsonplceholder๋ฅผ ์ด๋ ‡๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค!
    • ๋™๊ธฐ์ฒ˜๋Ÿผ... ์ฝ”๋“œ๋ฅผ ์งค ์ˆ˜ ์žˆ์ง€...? ๋˜ ์žฅ์ ์ด... ์˜ค๋ฅ˜ ์‹œ์ ์ด ๋” ์ •ํ™•ํ•˜๊ฒŒ ๋‚˜์˜จ๋‹ค...
    async function printUser() {
    try {
    // resolve ํ˜ธ์ถœ ๋˜๋ฉด.
    const response = await axios.get(
    "https://jsonplaceholder.typicode.com/posts/1"
    );
    const userId = response.data.userId;
    console.log(userId);
    } catch (error) {
    console.log(error); // rejected ํ˜ธ์ถœ๋˜๋ฉด.
    }
    }
    printUser();