ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • javascript
    ๐Ÿ“• javascript - async await
    2023. 9. 15. 15:14
    728x90

    ๐Ÿ“ async ์™€ await

    javascript์˜ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ํŒจํ„ด์ค‘ ์ œ์ผ ์ตœ๊ทผ์˜ ๋ฌธ๋ฒ•์œผ๋กœ ๊ธฐ์กด์˜ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋ฐฉ์‹์ธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์™€ ํ”„๋กœ๋ฏธ์Šค์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ณ  ๊ฐ€๋…์„ฑ ๋†’์€ ์ฝ”๋“œ๋ฅผ ์ง€์›

     

    ๐Ÿ“ async & await ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

    async function ํ•จ์ˆ˜๋ช…() {
        await ๋น„๋™๊ธฐ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ๋ช…();
    }

    ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€ ๋กœ์ง์ค‘ HTTP ํ†ต์‹ ์„ ํ•˜๋Š” ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์ฝ”๋“œ ์•ž์— await๋ฅผ ๋ถ™์ธ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ๊ฐ€ ๊ผญ ํ”„๋กœ๋ฏธ์Šค ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ await๊ฐ€ ์ •์ƒ ๋™์ž‘ํ•œ๋‹ค.

     

    ๐Ÿ“ example

    function mainExcel()
    {
        return new Promise(function(resolve, reject){
                $.ajax({
                    url: '/',
                type: "GET",
                data: postdata,
                success : function(data) {
                        resolve(data.result);
                },
                error : function(error){
                }
            });
        })
    }
    
    // ๋น„๋™๊ธฐ ์ˆœ์ฐจ ์‹คํ–‰
    async function excelDown(formId)
    {
        var chk_arr = ['A','B','C','D'];
        if(chk_arr.includes('A'))
            await mainExcel(formId,start_date,end_date,'A');
    
        if(chk_arr.includes('B'))
            await mainExcel(formId,start_date,end_date,'B');
    
        if(chk_arr.includes('C'))
            await mainExcel(formId,start_date,end_date,'C');
    
        if(chk_arr.includes('D'))
            await mainExcel(formId,start_date,end_date,'D');
    }

    excel ๋‹ค์šด๋กœ๋“œ์‹œ 4๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ์—‘์…€์„ ์ฐจ๋ก€๋กœ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๋Š” ๋กœ์ง.

     

    ๐Ÿ“ ์ฐธ๊ณ 

    728x90
Designed by Tistory.