async defer - amorfati0310/javascript-vm GitHub Wiki

javascript์ฝ”๋“œ๋ฅผ src๋กœ ์ง€์ •ํ•˜๊ณ  ์žˆ๋Š” script ํƒœ๊ทธ์˜ ์œ„์น˜๋Š” ๋ณดํ†ต body ํƒœ๊ทธ๊ฐ€ ๋‹ซํžˆ๊ธฐ ์ „์— ์œ„์น˜์‹œํ‚จ๋‹ค. ๊ทธ๋ž˜์•ผ DOMํŒŒ์‹ฑ์„ ๋ฐฉํ•ดํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ scriptํƒœ๊ทธ์—๋Š” async ์™€ defer๋ผ๋Š” ์†์„ฑ์ด ์žˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜๋ฉด DOM ๋กœ๋”ฉ์„ ๋ฐฉํ•ดํ•˜์ง€ ์•Š๊ณ , javascript ๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด์™„ ๊ด€๋ จ๋œ ์•„๋ž˜ ๊ธ€์„ ํ•™์Šตํ•œ๋‹ค. http://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html

์ด๊ธ€์— ๋‚˜์˜จ๊ฒƒ์ฒ˜๋Ÿผ, script์™€ dom ๋‹ค์šด๋กœ๋“œ์™€ ํŒŒ์‹ฑ์˜ ์ˆœ์„œ์™€ ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ๋ฐฉ๋ฒ•์„, async์™€ defer ์†์„ฑ์„ ํ†ตํ•ด์„œ ์„ ํƒ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ์ž˜ ํ™œ์šฉํ•ด์„œ scriptํƒœ๊ทธ๋ฅผ HTML์•ˆ์— ์œ„์น˜์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

asyncAnddefer

async

async๋Š” ๋™์‹œ์— htmlํŒŒ์‹ฑ๊ณผ ๋™์‹œ์— ๋‹ค์šด๋กœ๋“œ๋ฅผ ํ•˜๋‹ค๊ฐ€ script๊ฐ€ ์‹คํ–‰๋  ๋•Œ๋Š” htmlํŒŒ์‹ฑ์ด ์ž ์‹œ ๋ฉˆ์ถฅ๋‹ˆ๋‹ค. async downloads the file during HTML parsing and will pause the HTML parser to execute it when it has finished downloading.

defer

defer๋Š” ๋‹ค์šด๋กœ๋“œ๋Š” ์—ญ์‹œ ๋™์‹œ์— ์ง„ํ–‰๋˜๊ณ  ์‹คํ–‰ ์ž์ฒด๋ฅผ htmlํŒŒ์‹ฑ์ด ๋‹ค ๋๋‚œ๋‹ค์Œ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค

defer downloads the file during HTML parsing and will only execute it after the parser has completed. defer scripts are also guaranteed to execute in the order that they appear in the document.