๐Ÿฏ ์ฝ”์–ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ - 04 ์ฝœ๋ฐฑ ํ•จ์ˆ˜ (3)

modoleeยท2020๋…„ 9์›” 21์ผ
2
post-thumbnail

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

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

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

๋™๊ธฐ์™€ ๋น„๋™๊ธฐ

๋™๊ธฐ์ ์ธ ์ฝ”๋“œ

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

๋น„๋™์ ์ธ ์ฝ”๋“œ

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

์˜ˆ์ œ - ์ฝœ๋ฐฑ ์ง€์˜ฅ

setTimeout(function (name) {
  let coffeeList = name;
  console.log(coffeeList); // "์—์Šคํ”„๋ ˆ์†Œ"
  
  setTimeout(function (name) {
    coffeeList += ', ' + name;
    console.log(coffeeList); // "์—์Šคํ”„๋ ˆ์†Œ, ์•„๋ฉ”๋ฆฌ์นด๋…ธ"
    
    setTimeout(function (name) {
      coffeeList += ', ' + name;
      console.log(coffeeList); // "์—์Šคํ”„๋ ˆ์†Œ, ์•„๋ฉ”๋ฆฌ์นด๋…ธ, ์นดํŽ˜๋ชจ์นด"
      
      setTimeout(function (name) {
        coffeeList += ', ' + name;
        console.log(coffeeList); // "์—์Šคํ”„๋ ˆ์†Œ, ์•„๋ฉ”๋ฆฌ์นด๋…ธ, ์นดํŽ˜๋ชจ์นด, ์นดํŽ˜๋ผ๋–ผ"
      }, 500, '์นดํŽ˜๋ผ๋–ผ');
    }, 500, '์นดํŽ˜๋ชจ์นด');
  }, 500, '์•„๋ฉ”๋ฆฌ์นด๋…ธ');
}, 500, '์—์Šคํ”„๋ ˆ์†Œ');
  • ๋ชฉ์  ๋‹ฌ์„ฑ์—๋Š” ์ง€์žฅ์ด ์—†์ง€๋งŒ ๋“ค์—ฌ์“ฐ๊ธฐ ์ˆ˜์ค€์ด ๊ณผ๋„ํ•˜๊ฒŒ ๊นŠ์–ด์ง
  • ๊ฐ’์ด ์ „๋‹ฌ๋˜๋Š” ์ˆœ์„œ๊ฐ€ '์•„๋ž˜์—์„œ ์œ„๋กœ' ํ–ฅํ•˜๊ณ  ์žˆ์–ด ์–ด์ƒ‰ํ•˜๊ฒŒ ๋Š๊ปด์ง

์˜ˆ์ œ - ์ฝœ๋ฐฑ ์ง€์˜ฅ ํ•ด๊ฒฐ / ๊ธฐ๋ช… ํ•จ์ˆ˜๋กœ ๋ณ€ํ™˜

let coffeeList = '';

const addEspresso = function (name) {
  coffeeList = name;
  console.log(coffeeList); // "์—์Šคํ”„๋ ˆ์†Œ"
  setTimeout(addAmericano, 500, '์•„๋ฉ”๋ฆฌ์นด๋…ธ');
};

const addAmericano = function (name) {
  coffeeList += ', ' + name;
  console.log(coffeeList); // "์—์Šคํ”„๋ ˆ์†Œ, ์•„๋ฉ”๋ฆฌ์นด๋…ธ"
  setTimeout(addMocha, 500, '์นดํŽ˜๋ชจ์นด');
};

const addMocha = function (name) {
  coffeeList += ', ' + name;
  console.log(coffeeList); // "์—์Šคํ”„๋ ˆ์†Œ, ์•„๋ฉ”๋ฆฌ์นด๋…ธ, ์นดํŽ˜๋ชจ์นด"
  setTimeout(addLatte, 500, '์นดํŽ˜๋ผ๋–ผ');
};

const addLatte = function (name) {
  coffeeList += ', ' + name;
  console.log(coffeeList); // "์—์Šคํ”„๋ ˆ์†Œ, ์•„๋ฉ”๋ฆฌ์นด๋…ธ, ์นดํŽ˜๋ชจ์นด, ์นดํŽ˜๋ผ๋–ผ"
};

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

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

  • ES6(ES2015)์—์„œ Promise, Generator ๋“ฑ์ด ๋„์ž…
  • ES8(ES2017)์—์„œ async / await ์ด ๋„์ž…

Promise(1)

new Promise(function (resolve) {
  setTimeout(function () {
    const name = '์—์Šคํ”„๋ ˆ์†Œ';
    console.log(name);
    resolve(name);
  }, 500);
}).then(function (prevName) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      const name = prevName + ', ์•„๋ฉ”๋ฆฌ์นด๋…ธ';
      console.log(name);
      resolve(name);
    }, 500);
  });
}).then(function (prevName) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      const name = prevName + ', ์นดํŽ˜๋ชจ์นด';
      console.log(name);
      resolve(name);
    }, 500);
  });
}).then(function (prevName) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      const name = prevName + ', ์นดํŽ˜๋ผ๋–ผ';
      console.log(name);
      resolve(name);
    }, 500);
  });
})
  • new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•œ Promise์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœํ•  ๋•Œ ๋ฐ”๋กœ ์‹คํ–‰
  • ๊ทธ ๋‚ด๋ถ€์— resolve ๋˜๋Š” reject ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ตฌ๋ฌธ์ด ์žˆ์„ ๊ฒฝ์šฐ ๋‘˜ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „๊นŒ์ง€๋Š” then ๋˜๋Š” catch ๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š์Œ

Promise(2)

const addCoffee = function (name) {
  return function (prevName) {
    return new Promise(function (resolve) {
      setTimeout(function () {
        const newName = prevName ? (prevName + ', ' + name) : name;
        console.log(newName);
        resolve(newName);
      }, 500);
    });
  }
};

addCoffee('์—์Šคํ”„๋ ˆ์†Œ')()
  .then(addCoffee('์•„๋ฉ”๋ฆฌ์นด๋…ธ'))
  .then(addCoffee('์นดํŽ˜๋ชจ์นด'))
  .then(addCoffee('์นดํŽ˜๋ผ๋–ผ'))
  • ๋ฐ˜๋ณต์ ์ธ ๋‚ด์šฉ์„ ํ•จ์ˆ˜ํ™”ํ•ด์„œ ๋”์šฑ ์งง๊ฒŒ ํ‘œํ˜„

Generator

const addCoffee = function (prevName, name) {
  setTimeout(function () {
    coffeeMaker.next(prevName ? prevName + ', ' + name : name);
  }, 500);
};

const coffeeGenerator = function* () {
  const espresso = yield addCoffee('', '์—์Šคํ”„๋ ˆ์†Œ');
  console.log(espresso);
  
  const americano = yield addCoffee(espresso, '์•„๋ฉ”๋ฆฌ์นด๋…ธ');
  console.log(americano);
  
  const mocha = yield addCoffee(americano, '์นดํŽ˜๋ชจ์นด');
  console.log(mocha);
  
  const latte = yield addCoffee(mocha, '์นดํŽ˜๋ผ๋–ผ');
  console.log(latte);
};

const coffeeMaker = coffeeGenerator();
coffeeMaker.next();
  • ES6์˜ Generator๋ฅผ ์ด์šฉ
  • '*'์ด ๋ถ™์€ ํ•จ์ˆ˜๊ฐ€ Generator
  • Generator ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด Iterator๊ฐ€ ๋ฐ˜ํ™˜๋˜๊ณ , Iterator๋Š” next๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
  • next ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด Generator ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ๊ฐ€์žฅ ๋จผ์ € ๋“ฑ์žฅํ•˜๋Š” yield์—์„œ ํ•จ์ˆ˜์˜ ์‹คํ–‰์„ ๋ฉˆ์ถค
  • ์ดํ›„ ๋‹ค์‹œ next ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์•ž์„œ ๋ฉˆ์ท„๋˜ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๊ทธ ๋‹ค์Œ์— ๋“ฑ์žฅํ•˜๋Š” yield์—์„œ ํ•จ์ˆ˜์˜ ์‹คํ–‰์„ ๋ฉˆ์ถค
  • ๋น„๋™๊ธฐ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋Š” ์‹œ์ ๋งˆ๋‹ค next ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด Generator ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ ์†Œ์Šค๊ฐ€ ์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰ ๋จ

Promise + Async/Await

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

const coffeeMaker = async function () {
  let coffeeList = '';
  const _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();
  • ES8(ES2017)์—์„œ ๊ฐ€๋…์„ฑ์ด ๋›ฐ์–ด๋‚˜๋ฉด์„œ ์ž‘์„ฑ๋ฒ•๋„ ๊ฐ„๋‹จํ•œ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€
  • ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” ํ•จ์ˆ˜ ์•ž์— async ํ‘œ๊ธฐ, ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์‹ค์งˆ์ ์ธ ๋น„๋™๊ธฐ ์ž‘์—…์ด ํ•„์š”ํ•œ ์œ„์น˜๋งˆ๋‹ค await ํ‘œ๊ธฐ
  • await ๋’ค์˜ ๋‚ด์šฉ์ด Promise๋กœ ์ž๋™ ์ „ํ™˜, ํ•ด๋‹น ๋‚ด์šฉ์ด resolve ๋œ ์ดํ›„์—์•ผ ๋‹ค์Œ์œผ๋กœ ์ง„ํ–‰
profile
๊ธฐ์ดˆ๊ฐ€ ํƒ„ํƒ„ํ•œ ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž๋ฅผ ๊ฟˆ๊ฟ‰๋‹ˆ๋‹ค.

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