"์ฝ์"
// ์ปคํผ๋ฅผ ์ฃผ๋ฌธํ๋ ํ๋ก๋ฏธ์ค ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค
// ์์ฑ์์๋ ์ฝ์์ ์งํค๊ธฐ ์ํ resolve ํจ์์, ์ฝ์์ ์งํค์ง ๋ชปํ์ ๋๋ฅผ ๋๋นํ reject ํจ์(executor ํจ์)๋ฅผ ์ธ์๋ก ์ ๋ฌํ๋ค
const orderCoffee = new Promise((resolve, reject) => {
const requestObj = new XMLHttpRequest();
requestObj.open('GET', 'orderCoffee.txt');
requestObj.onreadystatechange = () => {
if (requestObj.readyState === 4 && requestObj.status === 200) {
const result = requestObj.responseText;
// resolve ๋ฉ์๋๊ฐ ์คํ๋๋ฉด then ๋ฉ์๋๊ฐ ์๋์ผ๋ก ํธ์ถ๋๋ค
resolve(result);
// resolve ๋ฉ์๋ ํธ์ถ์ด ์๋ ์ํ์์ reject ๋ฉ์๋๊ฐ ์คํ๋๊ฑฐ๋ ํต์ ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฉด catch ๋ฉ์๋๊ฐ ์๋์ผ๋ก ํธ์ถ๋๋ค
// reject(result);
}
};
requestObj.send();
});
// then ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋น๋๊ธฐ ์ฝ๋๋ฅผ ๋ง์น ๋๊ธฐ์ ์ธ ์ฝ๋์ฒ๋ผ ์์ฑํ ์ ์๋ค
// resolve ๋ฉ์๋๊ฐ ์คํ๋ ๋ ์ ๋ฌ๋ ์ธ์(๋น๋๊ธฐ ํต์ ๊ฒฐ๊ณผ)๋ then ๋ฉ์๋์ ์ฝ๋ฐฑํจ์์ ์ธ์๋ก ์ ๋ฌ๋๋ค
orderCoffee.then((asyncResult) => {
console.log(asyncResult);
console.log('์ฝ์์ด ์ด๋ฃจ์ด์ก์ต๋๋ค.');
return asyncResult;
}).catch((error) => { // then ๋ฉ์๋๋ ํ๋ผ๋ฏธ์ค ๊ฐ์ฒด๋ฅผ ๋ฐํํ๊ธฐ ๋๋ฌธ์ catch ๋ฉ์๋๋ฅผ ์ด์ด์ ์ฐ๋๊ฒ์ด ๊ฐ๋ฅํ๋ค
console.log(new Error('์ปคํผ์ฃผ๋ฌธ์ด ์ ์์ ์ผ๋ก ์ด๋ค์ง์ง ์์์ต๋๋ค.'));
})
๐ก resolve ํจ์์ ์ ๋ฌ๋๋ ์ธ์๋ ๋น๋๊ธฐ ํต์ ์ ๊ฒฐ๊ณผ๊ฐ ๋ง์ง๋ง promise์ ๋น๋๊ธฐ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ ์๋๋ค. promise์ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ then ๋ฉ์๋ ์์์ ์ด๋ฃจ์ด์ง๋ค. promise๋ ์๋ฒ ํต์ ๊ณผ ์ง์ ์ ์ธ ๊ด๋ จ์ ์๋ค.
XMLHttpRequest๋ฅผ ๋์ฒดํ ์๋ก์ด API ๋ฑ์ฅ
let result = fetch('https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json');
console.log(result); // ๋จผ์ ์คํ๋ผ์ Promise {<pending>}
fetch๋ ํจ์์ฒ๋ผ ๋ฐ๋ก ์คํํ ์ ์๋ค. fetch ํจ์๋ฅผ ์คํํ๋ฉด ๋ฐ๋ก ์ธ์๋ก ์ ๋ฌํ url์ ์ ๊ทผํ์ฌ ๋ฐ์ดํฐ๋ฅผ ๋ค์ด๋ก๋ ๋ฐ๋๋ค. ๋ค์ด๋ก๋๊ฐ ์๋ฃ๋๋ฉด ๋ฐ๋ก fulfilled ์ํ์ promise๊ฐ ๋ฐํ๋๋ค.
let result = fetch('https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json');
result.then((data) => {
console.log(data);
}
fulfilled ์ํ์ promise๋ฅผ ๋ฐํํ๊ธฐ ๋๋ฌธ์ then์ ํตํด ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ ์ฝ์์ ๋ณด๋ฉด ์ด๋ฒ์๋ Response๋ผ๋ ๊ฐ์ฒด๊ฐ ์ถ๋ ฅ๋๋ค. Response ๊ฐ์ฒด์๋ ์์ฒญ์ ๋ํ ์ข ํฉ์ ์ธ ์๋ต ์ ๋ณด๊ฐ ์ ์ฅ๋์ด ์๋ค.
์ฐ๋ฆฌ๊ฐ ํ์ํ๊ฒ์ JSON ๋ฐ์ดํฐ์ด๊ธฐ ๋๋ฌธ์ .json()
๋ฉ์๋๋ฅผ ํตํด ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๋ก ๋ณํ์ด ๊ฐ๋ฅํ promise ๊ฐ์ฒด๋ฅผ ๋ฐํ๋ฐ์ ์ ์๋ค. ๋๋ฌธ์ ์ฌ๊ธฐ์ ํ๋ฒ ๋ then ๋ฉ์๋๋ฅผ ์ด์ฉํด์ผ ์ฐ๋ฆฌ๊ฐ ํ์ํ ๋ฐ์ดํฐ๋ฅผ ๋ฝ์๋ผ ์ ์๋ค.
let result = fetch('https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json');
result.then((data) => {
return data.json()
})
.then((result) => {
console.log(result);
return result;
})
.catch((result) => {
console.log(new Error(result));
});
async ํค์๋๋ ์ด๋ค ํจ์๋ ํ๋ก๋ฏธ์ค ๊ฐ์ฒด๋ฅผ ๋ฐํํ๊ฒ ๋ง๋ค ์ ์๋ค. (์ฌ์ง์ด return ํค์๋๊ฐ ์์ด๋!)
async function test(){}
console.log(test()); // Promise {<fulfilled>: undefined}
await ํค์๋๋ ๋ฐ๋์ async ํจ์ ์์์๋ง ์ฌ์ฉํ ์ ์๋ค.
await๋ async ํจ์์์์ promise ๊ฐ์ฒด๋ฅผ ๋ฐํํ ๋๊น์ง ๋ค์ ์ฝ๋๋ฅผ ์คํ์ํค์ง ์๊ณ ๊ธฐ๋ค๋ฆฌ๊ฒ ๋ง๋ค์ด์ค๋ค. ๊ทธ๋ฆฌ๊ณ ํ๋ผ๋ฏธ์ค ๊ฐ์ฒด์ fulfilled ๊ฐ์ ๋ฐํํ๋ค.
async function message() {
let hello = await new Promise((resolve) => {
setTimeout(() => {
resolve('hello'); // hello ๋ฐํ
}, 100)
})
let world = await new Promise((resolve) => {
setTimeout(() => {
resolve('world'); // world ๋ฐํ
}, 100)
})
console.log(`${hello} ${world}`);
}
message();
// Promise {<pending>}
// hello world