MapleStory Finger Point

๐Ÿ”ด WEB/๐ŸŸก JavaScript

[JS] ๋น„๋™๊ธฐ (setTimeout, Callback, Promise, async/await)

HYEJU01 2024. 8. 26. 22:01
 

๋ชฉ์ฐจ

 

     

     

    ๐Ÿ“Œ ๋™๊ธฐ vs ๋น„๋™๊ธฐ

    ๋™๊ธฐ : ์ˆœ์„œ ๋ณด์žฅ (์ฝ”๋“œ๊ฐ€ ๋ธ”๋กœํ‚น ๋  ์ˆ˜ ์žˆ์Œ)

    ๋น„๋™๊ธฐ : ์ˆœ์„œ ๋ณด์žฅ X  (์ž‘์—… ์ข…๋ฃŒ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ์‹คํ–‰๋จ)

     

    ๐Ÿ“Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„ : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ 

     

    ๐Ÿ“Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋Ÿฐํƒ€์ž„ : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋˜๋Š” ํ™˜๊ฒฝ 

     


     

     

    ๐Ÿ“Œ setTimeout  

    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅ ํ•จ์ˆ˜ 

    delay ๋งŒํผ ์ง€์—ฐํ•ด์„œ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰์‹œํ‚จ๋‹ค.(๋น„๋™๊ธฐ์ž‘์—…)

    setTimeout(callback, delay, param1, param2, ...);

     

     

    ๊ธฐ๋ณธ์ ์ธ setTimeout ์‚ฌ์šฉ๋ฐฉ๋ฒ•

     

     

     

    ๋งค๊ฐœ๋ณ€์ˆ˜ ๋„˜๊ธฐ๊ธฐ

     

     

    setTimeout ์ทจ์†Œํ•˜๊ธฐ

     

     

    ์žฌ๊ท€ํ˜ธ์ถœํ•˜๊ธฐ

    `setInterval` ๊ณผ ๋น„์Šทํ•œ ๋™์ž‘์„ ํ•˜๋Š”๋ฐ 

    ์ด์ „์‹คํ–‰์ด ๋๋‚˜์•ผ๋งŒ ๋‹ค์Œ ์‹คํ–‰์„ ์˜ˆ์•ฝํ•˜๋ฏ€๋กœ ์‹คํ–‰ ๊ฐ„๊ฒฉ์ด ์ผ์ •ํ•˜๊ฒŒ ์œ ์ง€๋œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Œ (์•ˆ์ „ํ•จ)

     

     

     


     

     

    ๐Ÿ“Œ setInterval

    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์žฅ ํ•จ์ˆ˜ 

    ์ผ์ • ์‹œ๊ฐ„๋งˆ๋‹ค ๋ฐ˜๋ณต ์‹คํ–‰ํ•œ๋‹ค. (์ฃผ๊ธฐ์ ์ธ ์—…๋ฐ์ดํŠธ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ)

    setInterval(callback, delay, param1, param2, ...);

     

     

    ๊ธฐ๋ณธ์ ์ธ setInterval ์‚ฌ์šฉ๋ฒ•

     

     

    setInterval ์ค‘์ง€ 

     

     

     

    ๋งค๊ฐœ๋ณ€์ˆ˜ ์ „๋‹ฌ

     

     

     

     

    setInterval ์ค‘์ฒฉ ์‹คํ–‰

     setInterval ์„ ์ค‘๋ณต ์‹คํ–‰ํ•˜์ง€ ์•Š๋„๋ก ํ•˜๊ณ , setTimeout ์œผ๋กœ ๊ด€๋ฆฌํ•˜์—ฌ ์ œ์–ด์š”๋ง


     

    ๐Ÿ“Œ ์ฝœ๋ฐฑํ•จ์ˆ˜

     

    ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜

    ๋‚˜์ค‘์— ์‹คํ–‰๋  ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹

     

     


     

     

    ๐Ÿ“Œ Promise 

    ๋น„๋™๊ธฐ ์ž‘์—…์ด ๋๋‚ฌ์„ ๋–„ ์‹คํ–‰๋˜๋Š” ๊ฐ์ฒด

    ๋น„๋™๊ธฐ ์ž‘์—…์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด

    ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ๋‹จ์ ์„ ํ•ด๊ฒฐํž˜ (๊ฐ€๋…์„ฑ, ์ฝœ๋ฐฑ์ง€์˜ฅ)

     

     

    • Pending (๋Œ€๊ธฐ ์ƒํƒœ): ์ž‘์—…์ด ์•„์ง ์™„๋ฃŒ๋˜์ง€ ์•Š์€ ์ƒํƒœ
    • Fulfilled (์ดํ–‰ ์ƒํƒœ): ์ž‘์—…์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋œ ์ƒํƒœ
    • Rejected (๊ฑฐ๋ถ€ ์ƒํƒœ): ์ž‘์—…์ด ์‹คํŒจํ•œ ์ƒํƒœ

     

     

    • `.then()` : ์ž‘์—…์ด ์„ฑ๊ณตํ–ˆ์„ ๋•Œ ์‹คํ–‰๋˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜
    • `.catch()` : ์ž‘์—…์ด ์‹คํŒจํ–ˆ์„ ๋•Œ ์‹คํ–‰๋˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜

     

    • `resolve`์™€ `reject`๋ผ๋Š” ๋‘ ๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง„ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ์ธ์ž๋กœ ์ „๋‹ฌ๋จ

     

    const myPromise = new Promise((resolve, reject) => {
        let success = true;
        setTimeout(() => {
            if (success) {
                resolve("์ž‘์—… ์„ฑ๊ณต!");
            } else {
                reject("์ž‘์—… ์‹คํŒจ!");
            }
        }, 2000);
    });
    
    myPromise
        .then(result => console.log("ok", result))
        .catch(error => console.log("error", error));

     

     

    Promise ์—ฐ๊ฒฐ (์ฒด์ด๋‹)

    function task1() {
        return new Promise(resolve => {
            setTimeout(() => {
                console.log("์ž‘์—… 1 ์™„๋ฃŒ!");
                resolve();
            }, 1000);
        });
    }
    
    function task2() {
        return new Promise(resolve => {
            setTimeout(() => {
                console.log("์ž‘์—… 2 ์™„๋ฃŒ!");
                resolve();
            }, 1000);
        });
    }
    
    function task3() {
        return new Promise(resolve => {
            setTimeout(() => {
                console.log("์ž‘์—… 3 ์™„๋ฃŒ!");
                resolve();
            }, 1000);
        });
    }
    
    task1()
        .then(task2)
        .then(task3)
        .then(() => console.log("๋ชจ๋“  ์ž‘์—… ์™„๋ฃŒ!"));

     

     

     

    `Promise.all()` ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ž‘์—… ๋™์‹œ์— ์‹คํ–‰ํ•˜๊ธฐ

    const taskA = new Promise(resolve => setTimeout(() => resolve("A ์™„๋ฃŒ!"), 1000));
    const taskB = new Promise(resolve => setTimeout(() => resolve("B ์™„๋ฃŒ!"), 2000));
    const taskC = new Promise(resolve => setTimeout(() => resolve("C ์™„๋ฃŒ!"), 1500));
    
    Promise.all([taskA, taskB, taskC]).then(results => {
        console.log("๋ชจ๋“  ์ž‘์—… ์™„๋ฃŒ:", results);
    });

     

     

     


     

     

    ๐Ÿ“Œ async/await

    ๋น„๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ๋” ์‰ฝ๊ฒŒ ์ฝ์„ ์ˆ˜ ์žˆ๊ฒŒํ•จ

    • ` async` ํ•จ์ˆ˜๋Š” ํ•ญ์ƒ `Promise` ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • `Promise` ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ๋„ ๋‚ด๋ถ€์—์„œ ์ž๋™์œผ๋กœ `Promise` ๋กœ ๊ฐ์‹ธ์„œ ๋ฐ˜ํ™˜
    • `async function ํ•จ์ˆ˜์ด๋ฆ„() {}`

     

    • `await` async ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ, Promise ๊ฐ€ ์ฒ˜๋ฆฌ ๋ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐํ›„ ๊ทธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•จ
    • ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ๋™๊ธฐ์ฒ˜๋Ÿผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰๋จ.
    async function fetchData() {
      return "๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ์™„๋ฃŒ";
    }
    
    async function execute() {
      const result = await fetchData();
      console.log(result);
    }
    
    execute();

     

     

    function delay(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }
    
    async function run() {
        console.log("2์ดˆ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค...");
        await delay(2000);
        console.log("2์ดˆ ํ›„ ์‹คํ–‰๋จ!");
    }
    
    run();

     

     

     

    fetch() ์™€ async/await ์‚ฌ์šฉ

    async function fetchData() {
        try {
            let response = await fetch("https://jsonplaceholder.typicode.com/posts/1");
            let data = await response.json();
            console.log("๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ์™„๋ฃŒ:", data);
        } catch (error) {
            console.log("์—๋Ÿฌ ๋ฐœ์ƒ:", error);
        }
    }
    
    fetchData();

     

     

    ๋น„๋™๊ธฐ ์ž‘์—… ์—ฌ๋Ÿฌ๊ฐœ ์ฒ˜๋ฆฌ

    async function task1() {
        await new Promise(resolve => setTimeout(resolve, 1000));
        console.log("โœ… ์ž‘์—… 1 ์™„๋ฃŒ!");
    }
    
    async function task2() {
        await new Promise(resolve => setTimeout(resolve, 2000));
        console.log("โœ… ์ž‘์—… 2 ์™„๋ฃŒ!");
    }
    
    async function runTasks() {
        console.log("์ž‘์—… ์‹œ์ž‘!");
        await task1();
        await task2();
        console.log("๋ชจ๋“  ์ž‘์—… ์™„๋ฃŒ!");
    }
    
    runTasks();