The goals
๐ช๐ปWhat is "Async Code"?
โ๐ปWorking with Callbacks
๐๐ปPromises
๐๐ปasync / await
quiz
๋๊ธฐ ์ฝ๋์ ๋น๋๊ธฐ ์ฝ๋์ ์ฐจ์ด๋ ๋ฌด์์ผ๊น์?
-
๋๊ธฐ ์ฝ๋๋ ํ ๋จ์๋ก ์คํ๋๊ณ ๋น๋๊ธฐ ์ฝ๋๋ ์ผ๋ถ ์์ (์: ํ์ด๋จธ)์ด ์๋ฃ๋๋ฉด ์คํ๋ฉ๋๋ค.
-
๋๊ธฐ ์ฝ๋๋ ๋น๋๊ธฐ ์ฝ๋(์: ํ์ด๋จธ)๊ฐ ๋ฐ๊ฒฌ๋ ๋๊น์ง ์คํ๋๊ณ ์ด ๊ฒฝ์ฐ ํด๋น ์ฝ๋๊ฐ ์คํ์ด ์๋ฃ๋ ๋๊น์ง ์คํ์ด ์ผ์ ์ค์ง๋ฉ๋๋ค.
-
๋ ์ฌ์ด์๋ ์ฐจ์ด๊ฐ ์์ต๋๋ค.
์ด ์ฝ๋๋ ์ฝ์์ ๋ฌด์์ ์ถ๋ ฅํ ๊น์?
console.log('Starting!);
setTimeout(() => {
console.log('Timer completed!');
}, 10);
console.log('Finished!');
-
- Starting!
- Timer completed!
- Finished!
-
- Starting!
- Finished!
- Timer completed!
-
์ถ๋ ฅ ์์๊ฐ ํญ์ ๊ฐ์ ๊ฒ์ ์๋๋๋ค - CPU์ ์ฌ๋ฌ ๋ค๋ฅธ ์์ธ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋๋ค.
JavaScript๋ ๋น๋๊ธฐ ์ฝ๋ ์คํ์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ๊น์?
-
๋ค์ค ์ค๋ ๋๋ก ์ฒ๋ฆฌํ๊ณ ๊ธฐ๋ณธ ์คํฌ๋ฆฝํธ ์คํ์ด ์ฐจ๋จ๋์ง ์๋๋ก ๋ ์ค๋ ๊ฑธ๋ฆฌ๋ ์์ (์: ํ์ด๋จธ)์ ๋ณ๋์ ์ค๋ ๋๋ก ๋๊น๋๋ค.
-
๋จ์ผ ์ค๋ ๋๋ก ์ฒ๋ฆฌํ๊ณ ๋ ์ค๋ ๊ฑธ๋ฆฌ๋ ์์ (์: ํ์ด๋จธ)์ด ์๋ฃ๋ ๋๊น์ง ์คํฌ๋ฆฝํธ ์คํ์ ์ฐจ๋จํฉ๋๋ค.
-
๋จ์ผ ์ค๋ ๋์ด์ง๋ง ๋ ์ค๋ ๊ฑธ๋ฆฌ๋ ์์ (์: ํ์ด๋จธ)์ (๋ค์ค ์ค๋ ๋๋ฅผ ์ฌ์ฉํ๋) ๋ธ๋ผ์ฐ์ ๋ก ๋๊น๋๋ค.
Instead of having multiple levels and that makes our code way more readable.
If we put catch in the middle of the promise code, then all prior then would be skipped.
์ฌ๋ฌ๋ถ์ ๋ค์ํ ํ๋ก๋ฏธ์ค ์ํ์ ๋ํด ๋ฐฐ์ ์ต๋๋ค.
- PENDING => ํ๋ก๋ฏธ์ค๊ฐ ์๋ ์ค์ด๋ฉฐthen()์ด๋ catch() ๊ฐ ์คํ๋์ง ์์ต๋๋ค
- RESOLVED => ํ๋ก๋ฏธ์ค๊ฐ ํด๊ฒฐ๋์ต๋๋ค => then()์ด ์คํ๋ฉ๋๋ค
- REJECTED => · ํ๋ก๋ฏธ์ค๊ฐ ๊ฑฐ์ ๋์ต๋๋ค=> catch()์ด ์คํ๋ฉ๋๋ค
catch()๋ then() ๋ธ๋ก ๋ค์์ ๋ ๋ค๋ฅธ then() ๋ธ๋ก์ด ์์ผ๋ฉด ํ๋ก๋ฏธ์ค๊ฐ PENDING ๋ชจ๋๋ก ๋ค์ ๋ค์ด๊ฐ๋๋ค. (์ฐธ๊ณ : then()๊ณผ catch()๋ ํญ์ ์๋ก์ด ํ๋ก๋ฏธ์ค๋ฅผ ๋ฐํํฉ๋๋ค - ์ด๋ค ๊ฒ์ผ๋ก๋ ํด๊ฒฐ๋์ง ์๊ฑฐ๋then()๋ด๋ถ์์ returnํ ๊ฒ์ผ๋ก ํด๊ฒฐ๋ฉ๋๋ค). ๋ ์ด์ then() ๋ธ๋ก์ด ๋จ์ ์์ง ์์ ๊ฒฝ์ฐ์๋ง ์๋์ ์ต์ข ๋ชจ๋๋ก ๋ค์ด๊ฐ๋๋ค. SETTLED.
SETTLED๊ฐ ๋๋ฉด ํน์ ๋ธ๋ก์ธ finally()๋ก ์ต์ข ์ ๋ฆฌ ์์ ์ ์ํํ ์ ์์ต๋๋ค. ์ด์ ์ ํด๊ฒฐ๋๋ ๊ฑฐ๋ถ๋๋ ์๊ด์์ด finally()์๋ ๋๋ฌํฉ๋๋ค.
๋ค์์ ์์์ ๋๋ค:
somePromiseCreatingCode()
.then(firstResult => {
return 'done with first promise';
})
.catch(err => {
// would handle any errors thrown before
// implicitly returns a new promise - just like then()
})
.finally(() => {
// the promise is settled now - finally() will NOT return a new promise!
// you can do final cleanup work here
});
finally() ๋ธ๋ก์ ์ถ๊ฐํ ํ์ ์์ต๋๋ค(๊ฐ์์์ ํ์ง ์์์ผ๋๊น์).
Promise
Promise.all / Promise.race
Async / Await
quiz
JavaScript์์ ํ๋ก๋ฏธ์ค๋ ๋ฌด์์ผ๊น์?
-
ํ๋ก๋ฏธ์ค๋ JavaScript์์ ๋น๋๊ธฐ ์ฝ๋๋ฅผ ์๋์ํค๋ ์ ์ผํ ๋ฐฉ๋ฒ์ ๋๋ค.
-
ํ๋ก๋ฏธ์ค๋ ์์ ์ ๋ ์ฝ๊ฒ ํ๊ธฐ ์ํด (๋น๋๊ธฐ) ์ฝ๋๋ฅผ "๊ฐ์ธ๋" ๊ฐ์ฒด์ ๋๋ค.
-
ํ๋ก๋ฏธ์ค๋ Http ์์ฒญ์ ๋ฐฑ์๋ ์๋ฒ๋ก ๋ณด๋ด๊ณ ์๋ต์ผ๋ก ์์ ํ ์ ์๋๋ก ํฉ๋๋ค.
๋ง์ ๋น๋๊ธฐ ์์ ์ ๊ฒฝ์ฐ ์ฝ๋ฐฑ์ผ๋ก ์์ ํ ์๋ ์์ง๋ง ํ๋ก๋ฏธ์ค๋ฅผ ์ฌ์ฉํ๋ฉด ๋น๋๊ธฐ ์ฝ๋๋ก ์์ ํ๊ธฐ๊ฐ ๋ ์ฝ์ต๋๋ค.
ํ๋ก๋ฏธ์ค์ ๋งฅ๋ฝ์์ “์ฒด์ด๋(Chaining)”์ด๋ ๋ฌด์์ผ๊น์?
-
๊ฐ then() ๋ธ๋ก์ด ์ ํ๋ก๋ฏธ์ค๋ฅผ ๋ฐํํ ์ ์์ผ๋ฏ๋ก(๋ฐํํ ํ์๋ ์์) ์ฌ๋ฌ ํ๋ก๋ฏธ์ค๋ฅผ ์๋ก ์ฐ๊ฒฐํ ์ ์์ต๋๋ค.
-
์ด๋ ํ๋ก๋ฏธ์ค ๋ด์์ ๋ค์ํ ๋ฐฐ์ด ๋ฉ์๋(filter() ๋ฑ)๋ฅผ ์ฌ์ฉํ ์ ์์์ ์๋ฏธํฉ๋๋ค.
-
๊ฐ then()๊ณผ catch() ๋ฉ์๋๋ ์์ง SETTLED ๋์ด์ผ ํ๋ ์๋ก์ด ํ๋ก๋ฏธ์ค๋ฅผ ๋ฐํํ๊ธฐ ๋๋ฌธ์ then()๊ณผ catch() ๋ฉ์๋๋ฅผ ์๋ก์ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค.
์ค๋ต์ ๋๋ค. ๋ค์ ์๋ํ์ธ์.
return ๋ฌธ์ ์ถ๊ฐํ์ง ์๋๋ผ๋ then()๊ณผ catch()๋ ํญ์ ์๋ก์ด ํ๋ก๋ฏธ์ค๋ฅผ ์์ฑํฉ๋๋ค.
์๋ ์์์ ๋ง์ง๋ง then() ๋ธ๋ก์ด ์คํ๋ ๊น์(์ฆ ์ ๋ฌ๋ ํจ์๊ฐ ์คํ๋ ๊น์)?
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Timer completed!');
}, 1000);
})
.then((text) => { throw new Error('Failed!') })
.catch(err => console.log(err))
.then(() => console.log('Does that execute?'));
-
๊ทธ๋ ์ต๋๋ค!
-
์๋์ค, ์ด์ ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
-
์๋์ค, ํ๋ก๋ฏธ์ค ์ฒด์ธ๋น ๋ ๊ฐ ์ด์์ then()๊ณผ catch() ๋ธ๋ก์ ๊ฐ์ง ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
then()๊ณผ catch() ๋ธ๋ก์ด ์ํ๋ ๋งํผ ์์ ์ ์์ต๋๋ค.
catch() ๋ธ๋ก์ ํตํด ์ค๋ฅ๋ฅผ ์ฒ๋ฆฌํ๋ ํ ์ดํ์๋ then() ๋ธ๋ก์ด ์๋ํ ์ ์์ต๋๋ค. catch()๋ ๋ํ ์๋ก์ด ํ๋ก๋ฏธ์ค๋ฅผ ๋ฐํํฉ๋๋ค.
async/ await๊ณผ ํ๋ก๋ฏธ์ค์ ์ฐจ์ด์ ์ ๋ฌด์์ธ๊ฐ์?
-
async/ await๋ ๋น๋๊ธฐ ์ฝ๋๋ฅผ ์ฒ๋ฆฌ์ ๋ํด์๋ ์์ ํ ๋ค๋ฅธ ์ ๊ทผ ๋ฐฉ์์ด๊ณ ํ๋ก๋ฏธ์ค์ ์ ํ ๊ด๋ จ์ด ์์ต๋๋ค.
-
async/ await๋ฅผ ์ฌ์ฉํ๋ฉด ๋น๋๊ธฐ ์ฝ๋๋ฅผ ๋๊ธฐ ์ฝ๋๋ก ์ ํํ ์ ์์ต๋๋ค.
-
async/ await๋ ๊ฒฐ๊ตญ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์ฌ์ ํ ํ๋ก๋ฏธ์ค๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋ ๋ณํ์ ๋๋ค.
์๋์ ์ฝ๋์ ๋์ผํ ํ๋ก๋ฏธ์ค๋ ๋ฌด์์ผ๊น์?
async function wait() {
try {
const result = await doSomething();
console.log(result);
} catch (error) {
console.log('Error!');
}
}
-
- function wait() {
- doSomething()
- .catch(error => {
- console.log('Error!');
- })
- .then(result => {
- console.log(result);
- });
- }
-
- function wait() {
- doSomething()
- .then(result => {
- console.log(result);
- })
- .catch(error => {
- console.log('Error!');
- });
- }
-
- function wait() {
- doSomething()
- .then(result => {
- try {
- console.log(result);
- } catch (error) {
- console.log('Error!');
- }
- });
- }
'JavaScript - The Complete Guide 2022' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Section 19: Third-Party Libraries - Don't Re-Invent The Wheel! (0) | 2022.11.03 |
---|---|
Section 18: Network Requests - Sending Http Requests via JavaScript (0) | 2022.11.03 |
Section 16: Numbers & Strings - Deep Dive - Behind the Scenes & Beyond the Basics (0) | 2022.11.02 |
Section 15: Functions - Advanced Concepts - That's More (0) | 2022.11.01 |
Section 14: Events - Beyond "click" Listeners (0) | 2022.11.01 |