TIL

23/10/16 TIL __ ๋น„๋™๊ธฐ์  ์ฒ˜๋ฆฌ (promise, generator, async/await)

GABOJOK 2023. 10. 16. 15:35

 

 

 

๐Ÿš—  promise ํŒจํ„ด

 

  • new ์—ฐ์‚ฐ์ž๋กœ ํ˜ธ์ถœํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค. 
  • promise ํŒจํ„ด์˜ ์ธ์ž๋กœ ๋„˜์–ด๊ฐ€๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” ๋”ฐ๋กœ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„๋„ ๋ฐ”๋กœ ์‹คํ–‰๋œ๋‹ค. 
  • ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” resolve, reject ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค
  • ์„ฑ๊ณต์˜ ๊ฒฐ๊ณผ๋Š” resolve ๋ฅผ ํ†ตํ•ด then ์ด ์‹คํ–‰๋˜๋ฉฐ, ์‹คํŒจ์˜ ๊ฒฐ๊ณผ๋Š” reject๋ฅผ ํ†ตํ•ด catch ๊ฐ€ ์‹คํ–‰๋œ๋‹ค. 
  • catch๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธด๋‹ค. catch ๋ฅผ ์ด์šฉํ•ด ์—๋Ÿฌ๋ฅผ ์ปจํŠธ๋กค ํ•œ๋‹ค. 
  • .finally๋Š” ์„ฑ๊ณต ์‹คํŒจ ์—ฌ๋ถ€์™€ ์ƒ๊ด€ ์—†์ด ํ•ญ์ƒ ์‹คํ–‰๋œ๋‹ค. 

 

์˜ˆ์‹œ)

new Promise(function (resolve, reject) {
  setTimeout(function () {
    let name = "์—์Šคํ”„๋ ˆ์†Œ";
    console.log(name);
    resolve(name);
  }, 500);
})
  .then(function (preName) {
    return new Promise(function (resolve, reject) {
      setTimeout(function () {
        let name = preName + ", ์•„๋ฉ”๋ฆฌ์นด๋…ธ";
        console.log(name);
        resolve(name);
      }, 500);
    });
  })
  .then(function (preName) {
    return new Promise(function (resolve, reject) {
      setTimeout(function () {
        let name = preName + ", ์นดํŽ˜๋ชจ์นด";
        console.log(name);
        resolve(name);
      }, 500);
    });
  });

 

 

 

์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ฆฌํŽ™ํ† ๋ง ํ•œ๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ๋œ๋‹ค. 

let addCoffee = function (name) {
  //preName์€ ์ฒซ๋ฒˆ์งธ ํ”„๋กœ๋ฏธ์Šค ํŒจํ„ด์—์„œ ๋ฐ˜ํ™˜๋œ ๊ฐ’์ด ์ž๋™์œผ๋กœ ๋“ค์–ด์˜ด.
  return function (preName) {
    return new Promise(function (resolve, reject) {
      setTimeout(function () {
        let newName = preName ? preName + ", " + name : name;
        console.log(newName);
        resolve(newName);
      }, 500);
    });
  };
};

addCoffee("์—์Šคํ”„๋ ˆ์†Œ")()
  .then(addCoffee("๋…น์ฐจ๋ผ๋–ผ"))
  .then(addCoffee("๋ผ์ž„ํ”ผ์ง€์˜ค"));

 

์ฝ”๋“œ๋ฅผ ์ฒœ์ฒœํžˆ ์‚ดํŽด๋ณด๋ฉด ์ดํ•ด๋Š” ๊ฐ€์ง€๋งŒ, ์ง์ ‘ ๋ฆฌํŽ™ํ† ๋ง์„ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ๋” ๋งŽ์ด ๋ณด๊ณ  ์ณ๋ด์•ผ๊ฒ ๋‹ค.

 

  ๐Ÿ‘€  reject๊ฐ€ ๋˜์„œ catch๊ตฌ๋ฌธ์ด ์‹คํ–‰๋˜๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ์˜ˆ์‹œ๋Š” ์•„๋ž˜ ๋งํฌ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ์žˆ๋‹ค. 

https://gabojok.tistory.com/142

 

ES6 ํ”„๋กœ๋ฏธ์Šค ํŒจํ„ด !!("์ฝœ๋ฐฑ์ง€์˜ฅ" ์œผ๋กœ ๋ถ€ํ„ฐ ํƒˆ์ถœ)

์ฝœ๋ฐฑํ•จ์ˆ˜ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•˜๋ฉด ์–ด๋–ค ํ•จ์ˆ˜์˜ ์ธ์ž ๊ฐ’์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฑฐ๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ์ธ์ž๋ฅผ ํ˜ผ๋™ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ์— ์„ ์–ธํ•  ๋•Œ์— ์“ฐ์ด๋Š” ๊ฒƒ. ์ „๋‹ฌ์ธ์ž๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ

gabojok.tistory.com

 

 

 

 

 

๐Ÿš—  generator ํŒจํ„ด

 

  • iterator ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ํ•จ์ˆ˜์— * ์„ ๋ถ™์—ฌ์„œ ์ด์šฉํ•œ๋‹ค. 
  • ๋™๊ธฐ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ฝ์„ ์ˆ˜ ์žˆ๋„๋ก ํ•จ.   (๋น„๋™๊ธฐ ์ฝ”๋“œ ์ž์ฒด์˜ ๊ฐ€๋…์„ฑ ๋ฌธ์ œ )
  • generator ๋Š” ํ•จ์ˆ˜์˜ ์ฝ”๋“œ๋ธ”๋ก ์‹คํ–‰์„ ์ผ์‹œ ์ค‘์ง€ ํ•œ ์ดํ›„ , ํ•„์š”ํ•  ๋•Œ์— ๋‹ค์‹œ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • (es5 ๊นŒ์ง€๋Š” ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด ์™„๋ฃŒ๋  ๋•Œ ๊นŒ์ง€ ๊ณ„์† ์‹คํ–‰)
  • yield ๊ฐ€ ์‹คํ–‰๋˜๋ฉด ๐Ÿ‘‰ ์ œ์–ด๊ถŒ ์–‘๋ณด ๋˜๋ฉฐ, ํ•จ์ˆ˜๊ฐ€ ๋ฉˆ์ถ”๊ณ  ํ•จ์ˆ˜ ํ˜ธ์ถœ์ž ์—๊ฒŒ ์ œ์–ด๊ถŒ์„ ์–‘๋ณดํ•œ๋‹ค.
  • .next()๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ๋‹ค์‹œ ๋ฉˆ์ท„๋˜ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ๋‹ค. 
  •  ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง„๋‹ค.  (yield ์„ ๋งŒ๋‚˜ ์ •์ง€ ํ•œ ๋‹ค์Œ, ๋‹ค์‹œ ์‹คํ–‰ํ•˜๋ ค๋ฉด then ์„ ํ†ตํ•ด ์‹คํ–‰๋œ ๊ฒฐ๊ณผ๋ฌผ์„ ๋ฐ›์•„์™€์„œ .next() ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. )

 

 

์˜ˆ์‹œ)

let adCoffee = function (prevName, name) {
  setTimeout(function () {
    coffeeMaker.next(prevName ? prevName + ", " + name : name);
  }, 500);
};

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

//์ฝ˜์†”์ฐฝ
์—์Šคํ”„๋ ˆ์†Œ
์—์Šคํ”„๋ ˆ์†Œ, ์•„๋ฉ”๋ฆฌ์นด๋…ธ
์—์Šคํ”„๋ ˆ์†Œ, ์•„๋ฉ”๋ฆฌ์นด๋…ธ, ์นดํŽ˜๋ชจ์นด
์—์Šคํ”„๋ ˆ์†Œ, ์•„๋ฉ”๋ฆฌ์นด๋…ธ, ์นดํŽ˜๋ชจ์นด, ์นดํŽ˜๋ผ๋–ผ

 

 

์˜ˆ์‹œ)

var x = 1;

// ์ œ๋„ค๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด function ํ‚ค์›Œ๋“œ์— * ์„ ๋ถ™์ž…๋‹ˆ๋‹ค.
function* foo() {
  x++;
  yield;
  console.log("x : ", x);
}

function bar() {
  x++;
}

// ์ œ๋„ค๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉฐ, ์ œ๋„ค๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ œ๋„ค๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋ฉด์„œ, ๋™์‹œ์— ์ดํ„ฐ๋ ˆ์ดํ„ฐ์ž…๋‹ˆ๋‹ค.
// ๋ฐ˜ํ™˜๋œ 'generator' ๊ฐ์ฒด๋Š” next() ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
var generator = foo();

// foo()๋Š” ์—ฌ๊ธฐ์„œ ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค.
generator.next();
console.log(x); // 2
bar();
console.log(x); // 3
generator.next(); // x : 3

 

 

 

 

 

 

๐Ÿš—   async / await  ํŒจํ„ด

 

  • promise ์™€, generator๋ฅผ ํ•ฉ์นœ ์• !
  • ์–ธ์ œ๋‚˜ promise ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜. __ promise ํŒจํ„ด ๋งŒ๋“ค์ง€ ์•Š๊ณ ๋„ promise ํŒจํ„ด์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค. 
  • promise ํŒจํ„ด๊ณผ๋Š” ๋‹ฌ๋ฆฌ ํ•จ์ˆ˜ ์‹คํ–‰ํ•ด์•ผ ์ž‘๋™
  • await๊ฐ€ ๊ฑธ๋ ค์žˆ๋Š” ์ฝ”๋“œ๊ฐ€ ํ•ด๊ฒฐ๋  ๋•Œ ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค. 
  • await๋ฅผ ํ†ตํ•ด promise๊ฐ€ ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค. 
  • try { ์‹คํ–‰ํ•  ๊ตฌ๋ฌธ }, catch( err ){ ์—๋Ÿฌ ์ฒ˜๋ฆฌ } 

 

 

์˜ˆ์‹œ)

let adadCoffee = function (name) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve(name);
    }, 500);
  });
};

let coffeeMaker1 = async function () {
  let coffeeList = "";
  let _adadCoffe = async function (name) {
    coffeeList += (coffeeList ? ", " : "") + (await adadCoffee(name));
  };

  await _adadCoffe("์—์Šคํ”„๋ ˆ์†Œ");
  console.log(coffeeList);
  await _adadCoffe("์˜ค๋ Œ์ง€์ฃผ์Šค");
  console.log(coffeeList);
  await _adadCoffe("์‚ฌ๊ณผ์ฃผ์Šค");
  console.log(coffeeList);
};

coffeeMaker1();

 

 

์˜ˆ์‹œ)

async function main() {
  try {
    const response = await ajaxRequest('์ž˜๋ชป๋œ URL');
    render(response.data);
  } catch (error) {
    console.error(error) // ์—๋Ÿฌ ๋ฐœ์ƒ
  }
}

 

 

 

 

 

์ฐธ๊ณ ํ•œ ๋ธ”๋กœ๊ทธ

https://velog.io/@ctdlog/JS-%EC%9E%90%EB%B0%94-%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%9D%98-%EB%B9%84%EB%8F%99%EA%B8%B0-%EC%B2%98%EB%A6%AC-%ED%86%BA%EC%95%84%EB%B3%B4%EA%B8%B0-2-generator-asyncawait