ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [React] ๋น„๋™๊ธฐ(callback, promise, async/await)
    Front-end/react 2023. 1. 23. 19:00

     

    ๐Ÿ“Œ ๋น„๋™๊ธฐ ์ž‘์—…

    ์„œ๋ฒ„์˜ API๋ฅผ ์ด์šฉํ•  ๋•Œ๋Š” ๋„คํŠธ์›Œํฌ ์†ก์ˆ˜์‹  ๊ณผ์ •์—์„œ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์—
    ์ž‘์—…์ด ์ฆ‰์‹œ ์ฒ˜๋ฆฌ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์‘๋‹ต์„ ๋ฐ›์„ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ์ „๋‹ฌ๋ฐ›์€ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค.

    ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋ฉด ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋ฉˆ์ถ”์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋™์‹œ์— ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ์˜ˆ์‹œ) setTimeout ํ•จ์ˆ˜

    setTimeout() ์‹คํ–‰ ์‹œ์ ์— 3์ดˆ ๋™์•ˆ ๋ฉˆ์ถ”๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ผ๋‹จ ์ฝ”๋“œ๊ฐ€ ์œ„๋ถ€ํ„ฐ ์•„๋ž˜๊นŒ์ง€ ๋‹ค ํ˜ธ์ถœ๋˜๊ณ  3์ดˆ ๋’ค์— printMe() ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

    function printMe() {
      console.log("Hello World!");
    }
    
    setTimeout(printMe, 3000);
    console.log("๋Œ€๊ธฐ ์ค‘...");
    
    /*
    ๋Œ€๊ธฐ ์ค‘...
    Hello React!
    */

     

     

     

    ๋น„๋™๊ธฐ ์ž‘์—…์„ ํ•  ๋•Œ ๊ฐ€์žฅ ํ”ํžˆ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€

    โœจโœจ ์ฝœ๋ฐฑ ํ•จ์ˆ˜

    ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜

     

     

     

    ์˜ˆ์‹œ) ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ์ฃผ์–ด์ง€๋ฉด 1์ดˆ ๋’ค์— 10์„ ๋”ํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

    // ์ฝœ๋ฐฑ ํ•จ์ˆ˜
    // ex) ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์ด ์ฃผ์–ด์ง€๋ฉด 1์ดˆ ๋’ค์— 10์„ ๋”ํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
    function increase(number, callback) {
      setTimeout(() => {
        const result = number + 10;
        if (callback) {
          callback(result);
        }
      }, 1000);
    }
    
    increase(0, (result) => {
      console.log(result);
    });

     

    ์—ฌ๋Ÿฌ ๋ฒˆ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด? ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์ค‘์ฒฉํ•ด์„œ ๊ตฌํ˜„ํ•˜๊ธฐ

    // ์ฝœ๋ฐฑ ์ง€์˜ฅ
    console.log("์ž‘์—… ์‹œ์ž‘ ");
    increase(0, (result) => {
      console.log(result);
      increase(result, (result) => {
        console.log(result);
        increase(result, (result) => {
          console.log(result);
          console.log("์ž‘์—… ์‹œ์ž‘ ");
        });
      });
    });
    
    /* 
    ์ž‘์—… ์‹œ์ž‘
    10
    20
    30
    */

     

    ๐Ÿšจ ์ฝœ๋ฐฑ ์ง€์˜ฅ
    ์ฝœ๋ฐฑ ์•ˆ์— ๋˜ ์ฝœ๋ฐฑ์„ ๋„ฃ์œผ๋ฉด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ ธ์„œ ์ง€์–‘ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค

     

     

    โœจโœจ Promise

    ์ฝœ๋ฐฑ ์ง€์˜ฅ์„ ๋ฒ—์–ด๋‚˜๋Š” ๋ฐฉ๋ฒ•!

     

    Promise์—์„œ resolve๋œ ๊ฐ’์€ .then์„ ํ†ตํ•ด ๋ฐ›์•„ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค

    Promise๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ๋˜ .then์œผ๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ

     

    // Promise
    function increase2(number) {
      const promise = new Promise((resolve, reject) => {
        // resolve - ์„ฑ๊ณต, reject - ์‹คํŒจ
        setTimeout(() => {
          const result = number + 10;
          if (result > 50) {
            const e = new Error("์ˆซ์ž๊ฐ€ ์ปค์š”");
            return reject(e);
          }
          resolve(result);
        }, 1000);
      });
      return promise;
    }
    
    increase2(0)
      .then((number) => {
        console.log(number);
        return increase(number); // Promise๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ๋˜ .then์œผ๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ
      })
      .then((number) => {
        console.log(number);
        return increase(number);
      })
      .then((number) => {
        console.log(number);
        return increase(number);
      })
      .catch((e) => {
        console.log(e);
      });

     

     

    โœจโœจ async/await

    Promise๋ฅผ ๋”์šฑ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ํ‚ค์›Œ๋“œ

     

    ํ•จ์ˆ˜์˜ ์•ž๋ถ€๋ถ„์— async ํ‚ค์›Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ํ•ด๋‹น ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ Promise์˜ ์•ž๋ถ€๋ถ„์— await ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.์ด๋ ‡๊ฒŒ ํ•˜๋ฉด Promise๊ฐ€ ๋๋‚  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๊ณ , ๊ฒฐ๊ณผ ๊ฐ’์„ ํŠน์ • ๋ณ€์ˆ˜์— ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค.

     

    // async / await
    // Promise๊ฐ€ ๋๋‚  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๊ณ , ๊ฒฐ๊ณผ ๊ฐ’์„ ํŠน์ • ๋ณ€์ˆ˜์— ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค
    async function runTasks() {
      try {
        let result = await increase(0);
        console.log(result);
        result = await increase(result);
        console.log(result);
        result = await increase(result);
        console.log(result);
      } catch (e) {
        console.log(e);
      }
    }

     

     

     

     

     

     

Designed by Tistory.