230413 - async, await

๋ฐฑ์Šน์—ฐยท2023๋…„ 4์›” 13์ผ
1

๐Ÿšฉ async, await

async, await

๐Ÿ“ ์„ค๋ช…

  • promise๋ฅผ ์กฐ๊ธˆ ๋” ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฌธ๋ฒ•
  • async๋Š” function ์•ž์— ์œ„์น˜ํ•˜๊ณ , await๋Š” promise ์•ž์— ์œ„์น˜ํ•œ๋‹ค.


โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

js

// async, await (์ ˆ์ฐจ์ )
// .then์ด ํ•„์š” ์—†์Œ(์ฝ”๋“œ๊ฐ€ ์ง€์ €๋ถ„ํ•ด์ง€๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€)

// promise then ์‚ฌ์šฉ
function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
/*
console.log("ใ…Žใ…‡");
sleep(1000).then(() => console.log("ใ…Žใ…‡ใ…Žใ…‡"));
*/

// async, await ์‚ฌ์šฉ
async function process() { // ํ•จ์ˆ˜ ์ •์˜ ๋งจ ์•ž์— async ์ถ”๊ฐ€
  console.log("ใ…Žใ…‡");
  await sleep(1000); // ์ •์˜๋œ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ promise ์•ž์— await ์ถ”๊ฐ€
  console.log("ํ•˜์ด");
  return true; // ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ true
}

process() // ์‹œ๊ฐ„์ฐจ ์—†์ด ๊ทธ๋ƒฅ ์ž‘๋™ํ•จ
.then(console.log)

/*
// ๊ธฐ๋ณธ ์ƒํƒœ
// promise๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์™€ ๋˜‘๊ฐ™์ด ์ถœ๋ ฅ๋จ
console.log("ใ…Žใ…‡");

setTimeout(() => {
  console.log("ใ…Žใ…‡ใ…Žใ…‡");
}, 1000);
*/

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ





async, await

โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

js

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

async function makeError() {
  const error = new Error();
  throw error;
}

async function process() {
  try {
    await makeError()
  } 
  catch (aa) {
    console.error(aa)
  } 
  finally {

  }
}

process();

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ





async, await ์‘์šฉ

โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

js

// async ๋ถ™์ด๋ฉด ๋น„๋™๊ธฐ, ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ํ˜ธ์ถœ๋จ / ๋‚ด๋ถ€๋ฅผ ๋™๊ธฐ์ ์ธ ์ฝ”๋“œ๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅํ•˜๊ฒŒ

// then์ด ์ค‘๋ณต์œผ๋กœ ๋ฐฐ์น˜๋˜๊ณ  ๋” ๋ณต์žกํ•ด์ง -> ๊น”๋”ํ•˜๊ฒŒ ๋ณ€๊ฒฝ
// ๋น„๋™๊ธฐ์ ์ธ ์ฝ”๋“œ๊ฐ€ ๋™๊ธฐ์ (์ ˆ์ฐจ์ )์œผ๋กœ ๋ณด์ด๋„๋ก
function getBanana() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("๐ŸŒ");
    }, 3000);
  });
}

function getApple() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("๐ŸŽ");
    }, 2000);
  });
}

// ๋ฐ”๋‚˜๋‚˜์™€ ์‚ฌ๊ณผ๋ฅผ ํ•จ๊ป˜ ๊ฐ€์ง€๊ณ  ์™€์„œ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ฆ
async function fruits() {
  const banana = await getBanana(); // promise ์•ž์— await ๋ถ™์—ฌ์•ผ ํ•จ
  const apple = await getApple();
  // console.log([banana, apple]);
  return [banana, apple];
}

fruits().then((aa) => console.log(aa)); // 4์ดˆ ํ›„ ์ถœ๋ ฅ

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ





async, await ์‘์šฉ2

โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

js

function fetchEgg(chicken) {
  // return new Promise((resolve, reject) => {});
  return Promise.resolve(`${chicken}  -> ๐Ÿฅš`);
}

function friedEgg(egg) {
  return Promise.resolve(`${egg} -> ๐Ÿณ`);
}

function getChicken() {
  // return Promise.resolve(`๐ŸŒฟ -> ๐Ÿ”`);
  return Promise.reject(new Error("์น˜ํ‚จ์„ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์—†์Œ"));
}

/*
function makeFriedEgg() {
  return getChicken() // ๐ŸŒฟ -> ๐Ÿ”
    // ๋Œ€์ฒด๊ฐ’์„ ๋„ฃ์–ด์ค˜์„œ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋„ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜๋„๋ก ํ•จ.
    .catch((error) => {
      console.log(error.name);
      return "๐Ÿ”";
    }) 
    .then((chicken) => {
      return fetchEgg(chicken);
    }) // ๐ŸŒฟ -> ๐Ÿ”  -> ๐Ÿฅš
    .then((egg) => friedEgg(egg)) // ๐ŸŒฟ -> ๐Ÿ”  -> ๐Ÿฅš -> ๐Ÿณ
    .then(console.log);
    // .catch((error)=>console.log(error.name))
}
*/
// ์ž๋™์œผ๋กœ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋กœ ๋ณ€ํ™˜
async function makeFriedEgg() {
  let chicken;
  try {
    chicken = await getChicken(); // ๐ŸŒฟ -> ๐Ÿ”
  } catch (error) {
    console.log(error.name);
    chicken = "๐Ÿ”";
  }
  const egg = await fetchEgg(chicken);
  const data = await friedEgg(egg);
  return console.log(data);
  // .catch((error)=>console.log(error.name))
}

makeFriedEgg();

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

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