๐ย Reference
๐ย Chapter
const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'foo1');
});
const promise4 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'foo2');
});
Promise.all(
[promise1, promise2, promise3, promise4]).then((values) => {
console.log(values);
});
//output: Array [3, 42, "foo1","foo2"]
promise3, promise4์ ๊ฒฐ๊ณผ๋ ๊ฐ๊ฐ 1์ด๊ฐ ๊ฑธ๋ฆฌ์ง๋ง ๋์์ ํธ๋ฆฌ๊ฑฐ ๋๊ธฐ ๋๋ฌธ์ 2์ดํ์ ๊ฒฐ๊ณผ๊ฐ ๋์ค๋ ๊ฒ์ด ์๋ 1์ด ํ์ ๊ฒฐ๊ณผ๊ฐ ๋์ค๊ฒ ๋๋ ๊ฒ์ด๋ค.
promise.all()์ ์ฌ์ฉํ ๋ ์ฃผ์ํด์ผํ ์ ์ ๋ฐฐ์ด ๋ด ์์ ์ค ์ด๋ ํ๋๋ผ๋ ๊ฑฐ๋ถํ๋ฉด ์ฆ์ ๊ฑฐ๋ถํ๋ค๋ ๊ฒ์ด๋ค.
์์ 2
var p1 = new Promise((resolve, reject) => {
setTimeout(() => resolve('ํ๋'), 1000);
});
var p2 = new Promise((resolve, reject) => {
setTimeout(() => resolve('๋'), 2000);
});
var p3 = new Promise((resolve, reject) => {
setTimeout(() => resolve('์
'), 3000);
});
var p4 = new Promise((resolve, reject) => {
setTimeout(() => resolve('๋ท'), 4000);
});
var p5 = new Promise((resolve, reject) => {
reject(new Error('๊ฑฐ๋ถ'));
});
// .catch ์ฌ์ฉ:
Promise.all([p1, p2, p3, p4, p5])
.then(values => {
console.log(values);
})
.catch(error => {
console.log(error.message)
});
// ์ฝ์ ์ถ๋ ฅ๊ฐ:
// "๊ฑฐ๋ถ"