์ฝ์ํด์ค …
promise์ ํ์์ฑ
์๋ฐ์คํฌ๋ฆฝํธ๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ํ ํจํด์ผ๋ก ์ฝ๋ฐฑํจ์๋ฅผ ์ฌ์ฉํ๊ณค ํฉ๋๋ค.
๊ทธ๋ฐ๋ฐ ์ด๋ฌํ ์ฝ๋ฐฑ ํจํด์ ๋ง์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋น๋๊ธฐ๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ์ฝ๋ฐฑํจ์๋ค์ด ์ง์ฅ์ ๋ง๋ค์ด ๋ด๊ธฐ ๋๋ฌธ์ด์ฃ .
์ด๋ฐ์์ผ๋ก์ ๋ณด๊ธฐ๋งํด๋ ์ฝ๊ณ ์ถ์ง ์์์ง๊ฒ ๋ง๋๋ ๋ง๋ฒ์ ๋ถ๋ฆฐ๋ต๋๋ค.
๋ํ ๋น๋๊ธฐ ํจ์๋ฅผ ํธ์ถํ๋ฉด ํจ์ ๋ด๋ถ์ ๋น๋๊ธฐ๋ก ๋์ํ๋ ์ฝ๋๊ฐ ์๋ฃ๋์ง ์์๋ค๊ณ ํด๋ ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ์ฆ์ ์ข ๋ฃ๋ฉ๋๋ค.
๋ํ ์๋ฌ ์ฒ๋ฆฌ์๋ ๋ถํธํจ์ ์ฃผ์ฃ .
์ด๋ฌํ ํ๋ก๋ฏธ์ค์ ํ๊ณ์ ๋ถํธํจ์ผ๋ก ์ธํด
ES6์์๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ํ ๋ค๋ฅธ ํจํด์ผ๋ก promise๋ฅผ ๋์ ํ์ต๋๋ค.
promise๋?
“A promise is an object that may produce a single value some time in the future”
ํ๋ก๋ฏธ์ค๋ ์๋ฐ์คํฌ๋ฆฝํธ ๋น๋๊ธฐ ์ฒ๋ฆฌ์ ์ฌ์ฉ๋๋ ๊ฐ์ฒด์ ๋๋ค. ์ฌ๊ธฐ์ ์๋ฐ์คํฌ๋ฆฝํธ์ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ ‘ํน์ ์ฝ๋์ ์คํ์ด ์๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ๋ค์ ์ฝ๋๋ฅผ ๋จผ์ ์ํํ๋ ์๋ฐ์คํฌ๋ฆฝํธ์ ํน์ฑ’์ ์๋ฏธํฉ๋๋ค.
promise ์์ฑ
Promise ์์ฑ์ ํจ์๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ํํ ์ฝ๋ฐฑํจ์๋ฅผ ์ธ์๋ก ์ ๋ฌ ๋ฐ๋๋ฐ,
์ฝ๋ฐฑ ํจ์๋ resolve์ reject ํจ์๋ฅผ ์ธ์๋ก ์ ๋ฌ ๋ฐ์ต๋๋ค.
const promise = new Promise((resolve, reject) => {
// promise ํจ์์ ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์์ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ์ํํ๋ค.
if (/* ๋น๋๊ธฐ ์ฒ๋ฆฌ ์ฑ๊ณต */) {
resolve('result');
} else /* ๋น๋๊ธฐ ์ฒ๋ฆฌ ์คํจ */ {
reject('fail')
}
})
์ด๋ฐ ์์ผ๋ก ์์ฑ์ ํฉ๋๋ค,
ํ๋ก๋ฏธ์ค๋ ๋น๋๊ธฐ์ ๋ํ ์ฒ๋ฆฌ๋ฅผ 3๊ฐ์ง ์ํ์ ๋ณด๋ก ๊ฐ์ต๋๋ค.
์์ฑ๋ ์งํ์ ํ๋ก๋ฏธ์ค์ ์ํ๋ ๊ธฐ๋ณธ์ ์ผ๋ก pending์ ๋๋ค.
์ดํ ๋น๋๊ธฐ ์ฒ๋ฆฌ๊ฐ ์ํ๋๋ฉด ๋น๋๊ธฐ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ์ ๋ฐ๋ผ ์ํ๊ฐ ๋ณ๊ฒฝ๋ฉ๋๋ค.
ํ๋ก๋ฏธ์ค์ ์ํ๋ resolve or reject ํจ์๋ฅผ ํธ์ถ ํ๋ ๊ฒ์ผ๋ก ๊ฒฐ์ ๋ฉ๋๋ค.
fulfiled ๋๋ rejected๋ ์ํ๋ฅผ settled์ํ๋ผ๊ณ ํฉ๋๋ค. settled์ํ๋ ์คํจ ์ฑ๊ณต์ฌ๋ถ์ ์๊ด์์ด ๋น๋๊ธฐ ์ฒ๋ฆฌ๊ฐ ์ํ๋ ์ํ๋ฅผ ๋งํฉ๋๋ค.
ํ๋ก๋ฏธ์ค์ ํ์ ์ฒ๋ฆฌ ๋ฉ์๋
์ ์ด์ ๋ฐ์์จ ๊ฐ๋ค์ ์ด๋ค ์์ผ๋ก ์ฒ๋ฆฌํด์ค์ผ ํ ๊น์?.
ํฌ๊ฒ ์ํ๋ fulfiled์ reject์ ๋ฐ๋ผ ์ฒ๋ฆฌ ๋ฉ๋๋ค.
promise.prototype.then
then์ ๋๊ฐ์ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ธ์๋ก ์ ๋ฌ ๋ฐ์ต๋๋ค.
- ์ฒซ๋ฒ์งธ ์ฝ๋ฐฑ ํจ์๋ ํ๋ก๋ฏธ์ค๊ฐ fulfiled๋๋ฉด ํธ์ถ๋ฉ๋๋ค. ์ด๋ ์ฝ๋ฐฑ ํจ์๋ ํ๋ก๋ฏธ์ค์ ๋น๋๊ธฐ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ธ์๋ก ์ ๋ฌ ๋ฐ์ต๋๋ค.
- ๋๋ฒ์งธ ์ฝ๋ฐฑ ํจ์๋ ํ๋ก๋ฏธ์ค๊ฐ rejecte๋๋ฉด ํธ์ถ๋ฉ๋๋ค. ์ด๋ ์ฝ๋ฐฑ ํจ์๋ ํ๋ก๋ฏธ์ค์ ์๋ฌ๋ฅผ ์ธ์๋ก ์ ๋ฌ ๋ฐ์ต๋๋ค
// fulfiled
new Promise(resolve => resolve('fulfiled'))
.then(v => console.log(v), e => console.log(e));
// rejected
new Promise((_, reject) => reject(new Error('rejected')))
.then(v => console.log(v), e => console.log(e));
promise.prototype.catch
์ด๋ฒ์ ์๊ฐํ ๋ฉ์๋๋ ํ๊ฐ์ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ธ์๋ก ๋ฐ์ต๋๋ค. catch ๋ฉ์๋์ ์ฝ๋ฐฑ ํจ์๋ promise๊ฐ rejected์ํ์ธ ๊ฒฝ์ฐ์๋ง ํธ์ถ ๋ฉ๋๋ค.
// rejected
new Promise((_, reject) => reject(new Error('rejected')))
.catch(e => console.log(e));
ํด๊ฒฐ
์ ๊ทธ๋์ ์ฒ์์ ๋งํ ๋ฌธ์ ์ ์ค์ ๊ฐ๋ ์ฑ์ ํด์น๋ ๋น๋๊ธฐ ์ฝ๋๋ ์ด๋ป๊ฒ ํด๊ฒฐํ ์ ์์๊น์?
๋ค์์ ํ์ผ์ ์ฝ์ด ํน์ ‘ํค์๋’๋ฅผ ์ฐพ๋ ์ฝ๋์ ๋๋ค.
์ฐธ ์ง์ ๋ถํ๊ฒ ์ฎ์ฌ์์ฃ ?
const fs = require('fs');
fs.readFile('file1.txt', 'utf8', (err, data1) => {
if (err) {
console.error(err);
} else {
fs.readFile('file2.txt', 'utf8', (err, data2) => {
if (err) {
console.error(err);
} else {
fs.readFile('file3.txt', 'utf8', (err, data3) => {
if (err) {
console.error(err);
} else {
if (data1.includes('keyword') && data2.includes('keyword') && data3.includes('keyword')) {
console.log('Keyword found in all files');
} else {
console.log('Keyword not found in all files');
}
}
});
}
});
}
});
const fs = require('fs');
function readFilePromise(filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, 'utf8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
readFilePromise('file1.txt')
.then((data1) => {
return readFilePromise('file2.txt')
.then((data2) => {
return [data1, data2];
});
})
.then(([data1, data2]) => {
return readFilePromise('file3.txt')
.then((data3) => {
return [data1, data2, data3];
});
})
.then(([data1, data2, data3]) => {
if (data1.includes('keyword') && data2.includes('keyword') && data3.includes('keyword')) {
console.log('Keyword found in all files');
} else {
console.log('Keyword not found in all files');
}
})
.catch((error) => {
console.error(error);
});
promise๋ฅผ ํตํด ๊ฐ๋ ์ฑ์ ๋์ธ ์ฝ๋์ ๋๋ค.
์๋์ ์ผ๋ก ์ฝ๋์ ๊ธธ์ด๋ ๋์ด๋ฌ์ผ๋, ์ฝ๊ธฐ ํจ์ฌ ์์ํ ์ฝ๋์ ๋๋ค.
๋ค์๋ฒ์ async์ await์ ๋ํด ์ ๋ฆฌํด ๋ณด๊ฒ ์ต๋๋ค.
https://joshua1988.github.io/web-development/javascript/promise-for-beginners/
'๊ฐ๋ฐ ์๋๋ธ๋ก' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
github action .env file ์ถ๊ฐ (0) | 2023.08.11 |
---|---|
์น ํ์ด์ง ์ฑ๋ฅํฅ์ (0) | 2023.08.02 |
๋ธ๋ผ์ฐ์ ๋ ๋๋ง (1) | 2023.05.13 |
ํํ ํ ํ์ ์ด๋? (0) | 2023.04.30 |
this (1) | 2023.04.23 |