๐Ÿค™Promise

TODO

2 minute read

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 ๋ฐ˜๋ณตํ•˜๋Š”๊ฒƒ์„ ์ฒด์ด๋‹์ด๋ผ๊ณ  ํ•˜๊ณ , ์œ ์ฆˆ์ผ€์ด์Šค๋Š”

  1. ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์™€์„œ 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

์š”๊ฒƒ๋„ ๋ด๋ณด์ž ํ•„์š”ํ•˜๋ฉด ์ •๋ฆฌ