๐คPromise
TODO
https://programmingsummaries.tistory.com/325
์ด๋ฏธ์ง ์ธ๋ค์ผ ์ญ์
[JavaScript] ๋ฐ๋ณด๋ค์ ์ํ Promise ๊ฐ์ - ๋๋์ฒด Promise๋ ์ด๋ป๊ฒ ์ฐ๋๊ฑฐ์ผ?
๋ค์ด๊ฐ๋ฉฐ JavaScript์ ์ธ๊ณ์์๋ ๊ฑฐ์ ๋๋ถ๋ถ์ ์์
๋ค์ด ๋น๋๊ธฐ๋ก ์ด๋ฃจ์ด์ง๋ค. ์ด๋ค ์์
์ ์์ฒญํ๋ฉด์ ์ฝ๋ฐฑ ํจ์๋ฅผ ๋ฑ๋กํ๋ฉด, ์์
์ด ์ํ๋๊ณ ๋์ ๊ฒฐ๊ณผ๋ฅผ ๋์ค์ ์ฝ๋ฐฑ ํจ์๋ฅผ ํตํด ์๋ ค์ฃผ๋ ์์ด๋ค. ์ค์ ๋น..
programmingsummaries.tistory.com
Promiss/ async-awiat ๋ฌธ๋ฒ๋ ์ค์ํ์ง๋ง, ๋น๋๊ธฐ๋ฐฉ์ ์์ฒด๋ฅผ ์ ์ดํดํด๋ณด์
const amIsexy = new Promise((resolve, reject) => {
setTimeout(resolve, 3000, โYes you areโ);
});
console.log(amIsexy);
setInterval(console.log, 1000, amIsexy);
์ด๋ฌ๋ฉด 3์ด์๋ค๊ฐ๋ถํฐ๋ ๊ฐ์ด ์ถ๋ ฅ๋๋ค. ๊ธฐ๋ณธ ์๋ฆฌ ์ดํด.
const amIsexy = new Promise((resolve, reject) => {
setTimeout(resolve, 3000, โYes you areโ); // resolve(โYes you areโ);
}); // ๋น๋๊ธฐ๋ก ํ ์ผ ํ๊ณ Resolve()๋ Reject() ํธ์ถ
amIsexy.then((value) => console.log(value));
const thenFn = (value2) => console.log(value2);
amIsexy.then(thenFn);
const errIsexy = new Promise((resolve, reject) => {
setTimeout(reject, 3000, โyou are uglyโ);
});
errIsexy.catch((value3) => console.log(value3));
errIsexy
.then((result) => console.log(result))
.catch((error) => console.log(error));
resolve ํธ์ถ์ then๋ถ๋ถ ์คํ
reject ํธ์ถ์ catch ๋ถ๋ถ ์คํ
then์ดํ catch๊ฐ ์คํ๋๋๊ฒ ์๋๋ผ๋๊ฑฐ ์ฃผ์
const amIsexy = new Promise((resolve, reject) => {
resolve(2);
});
amIsexy
.then((number) => {
console.log(number);
return number;
})
.then((othernumber) => {
console.log(othernumber * 2);
});
const amIdouble = new Promise((resolve, reject) => {
resolve(2);
});
const timesTwo = (numbertwo) => numbertwo * 2;
amIdouble
.then(timesTwo)
.then(timesTwo)
.then(timesTwo)
.then(timesTwo)
.then(() => {
throw Error(โsomething is wrongโ);
})
.then((lastNumber) => console.log(lastNumber))
.catch((error) => console.log(error));
promise์ promise ๋ฐ๋ณตํ๋๊ฒ์ ์ฒด์ด๋์ด๋ผ๊ณ ํ๊ณ , ์ ์ฆ์ผ์ด์ค๋
- ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์์ 2. ๊ทธ๊ฑธ ๋ณตํธํํ๊ณ ๋ญ ์ด๋ฐ๊ฑฐ
const p1 = new Promise((resolve) => {
setTimeout(resolve, 3000, โFirstโ);
});
const p2 = new Promise((resolve, reject) => {
setTimeout(reject, 2000, โsecondโ);
});
const p3 = new Promise((resolve) => {
setTimeout(resolve, 1000, โthirdโ);
});
const motherPromise = Promise.all([p1, p2, p3]);
motherPromise
.then((value) => console.log(value))
.catch((err) => console.log(err)); // p1, p2, p3์คํ๋๋ผ๋ ๋ฆฌ์ ๋๋ฉด ์ฌ๊ธฐ๋ก
์ฌ๋ฌ promise๊ฐ ์์ฐจ์ ์ผ๋ก ์คํ๋์ด์ผํ ๊ฒฝ์ฐ๋ ์์๊ฒ์ด๋ค.
์ด๋ด๋ Then์ผ๋ก ๊ณ์ ์ฐ๊ฒฐํ๋๊ฒ๋ณด๋ค Promise.all์ ์ฌ์ฉํด๋ณด์
all์์ promise๋ค์ ๋๋์์๋๋ก๊ฐ ์๋๋ผ all์์ ๋ฑ๋ก๋ ์์๋๋ก ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋๋ค(๋ฐฐ์ด๋ก)
๋ํ ํ๋๋ผ๋ Reject๋๋ฉด ์ ์ฒด๊ฐ Reject๋๋ค. ๋ค๋ฅธ ํ๋ก๋ฏธ์ค๋ ๋ค ๋ฆฌ์ ๋๋ค.
const motherPromise = Promise.race([p1, p2, p3]); // ์ ์์ ์์ all์ด race๋ก ๋ฐ๋
motherPromise
.then((value) => console.log(value))
.catch((err) => console.log(err)); // p1, p2, p3์คํ๋๋ผ๋ ๋ฆฌ์ ๋๋ฉด ์ฌ๊ธฐ๋ก
๋ง๊ทธ๋๋ก ๋ ์ด์ค์ด๋ค. ๋ฐฐ์ด์ ํ๋ก๋ฏธ์ค ์ค ๊ฐ์ฅ ๋นจ๋ฆฌ ๋๋๋ reject์ด๋ resolve ํ๋๋ง ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค.
์ ์์ ์ ๊ฒฝ์ฐ p3๊ฐ 1์ด๋ก ๊ฐ์ฅ ๋น ๋ฅด๋ฏ๋ก p3๋ง ๋ฐ๊ฒ์ด๋ค.๏ฟฆ
const p1 = new Promise((resolve, reject) => {
setTimeout(reject, 1000, โFirstโ);
})
.then((value) => console.log(value))
.catch((e) => console.log(${e}error
))
.finally(() => console.log(โIm doneโ));
try catch์ฒ๋ผ ๋น ์ด๋๋ฆฌ ํ๋์ผ
fetch(โhttp://127.0.0.1:5500/index.htmlโ)
.then((response) => response.text())
// .then((potato) => console.log(potato))
.catch((e) => console.log(โ${e}
));
fetch(โhttps://yts.mx/api/v2/list_movies.jsonโ)
.then((response) => {
//console.log(response);
return response.json();
})
.then((potato) => console.log(potato))
.catch((e) => console.log(โ${e}
));
์ค์ ์ฌ์ฉ๋ก
http://junil-hwang.com/blog/javascript-promise-async-await/
์ด๋ฏธ์ง ์ธ๋ค์ผ ์ญ์
[javascript] Promise, async, await - ๊ฐ๋ฐ์ ํฉ์ค์ผ
javascript๋ event driven ์ผ๋ก ์๋๋๋ฉฐ ์ด์ ๋ฐ๋ผ ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ ํด์ผํฉ๋๋ค. ์ฆ, ์ด๋ฒคํธ๊ฐ ๋๋ ํ์ ์ฝ๋๊ฐ ์คํ ๋๋ ๋ฐฉ์์
๋๋ค. ์ด ๋ ๋ง์ ์ฌ๋๋ค์ด callback ์ง์ฅ์ ์ง๋ฉดํ๊ฒ ๋๋ฉฐ, promise๋ฅผ ํตํด ํด๊ฒฐํ ์ ์์ต๋๋ค.
junil-hwang.com
์๊ฒ๋ ๋ด๋ณด์ ํ์ํ๋ฉด ์ ๋ฆฌ