๋ชฉ์ฐจ
catch ๋ฉ์๋
๊ธฐ๋ณธ์ ์ผ๋ก ์์ธ์ํฉ์ ์ฒ๋ฆฌํ๋ค.
๋ง์ฝ
failCallback์ด ์ ์๋์ด ์์ง ์์ ๋์๋
reject ์์ catch ๋ฉ์๋ ํธ์ถ๋๋ค.
(์์ธ์ํฉ์ผ๋ก ์ธ์)
const func = new Promise(
(resolve, reject) => { //์ฑ๊ณต, ์คํจ ํจ์์ธ์๋ก ๋ฐ๊ธฐ
setTimeout(() => { //๋น๋๊ธฐ ํจ์ ์คํ
let num = 10
if (num > 11) {
resolve(num) //successCallback
} else {
reject("error") //failCallback
}
}, 1000);
}
)
func
.then((item) => { //successCallback
console.log('success', item)
})
.catch((err) => {
console.log(err)
})
>>>
error
throw
๊ฐ์ ๋ก ์ฌ์ฉ์ ์ ์ ์์ธ ๋ฐ์ ์ํจ๋ค.
catch๋ธ๋ก์ด ์๋ค๋ฉด ๊ทธ ๊ณณ์ผ๋ก ์ ๋ฌ๋๊ณ
๊ทธ๋ ์ง ์์ผ๋ฉด ํ๋ก๊ทธ๋จ์ ์ข ๋ฃํ๋ค.
throw new Error("์๋ฌ๋ฉ์ธ์ง")
chaining
.then .catch ๋ฑ์ผ๋ก ์ด์ด์ ์ฝ๋๋ฅผ ์์ฑํ๋ ๊ฒ์ ๋งํ๋ค.
chaining - catch
์์ธ๊ฐ ๋ฐ์ํ๋ฉด chaining๋ ๋ค์ ์ฝ๋๋ฅผ ๋์ํ์ง ์๊ณ
catch๋ก ๋์ด๊ฐ๋ค.
const func = new Promise(
(resolve, reject) => { //์ฑ๊ณต, ์คํจ ํจ์์ธ์๋ก ๋ฐ๊ธฐ
setTimeout(() => { //๋น๋๊ธฐ ํจ์ ์คํ
let num = 10
if (num > 9) {
resolve(num) //successCallback
} else {
reject("error") //failCallback
}
}, 1000);
}
)
func
.then((item) => { //successCallback
console.log('success-1')
throw new Error("๋ด") //์์ธ๋ฐ์
})
.then(() => {
console.log('success-2') //๊ฑด๋๋ฐ๊ธฐ
})
.catch(() => {
console.log("err")
})
>>>
success-1
err
chaining-return
chaining์ด ๋์ด์์ ๋
return ๊ฐ์ ๋ค์ chaining์ ์ธ์๋ก ์ ๋ฌ๋๋ค.
const func = new Promise(
(resolve, reject) => { //์ฑ๊ณต, ์คํจ ํจ์์ธ์๋ก ๋ฐ๊ธฐ
setTimeout(() => { //๋น๋๊ธฐ ํจ์ ์คํ
let num = 10
if (num > 9) {
resolve(num) //successCallback
} else {
reject("error") //failCallback
}
}, 1000);
}
)
func
.then((item) => { //successCallback
console.log('success-1',item)
return 1 //๋ฆฌํด๊ฐ 1
})
.then((num) => {
console.log('success-2',num) //์ธ์๋ฅผ ์ถ๋ ฅํ์ ๋ 1
})
>>>
success-1 10
success-2 1
finally
Promise๊ฐ resolve๋๋ reject๋๋ ์๊ด์์ด
๋ง์ง๋ง์ ์คํ๋๋ ํจ์
๋จผ์ resolve ๋์์ ๋
const func = new Promise(
(resolve, reject) => { //์ฑ๊ณต, ์คํจ ํจ์์ธ์๋ก ๋ฐ๊ธฐ
setTimeout(() => { //๋น๋๊ธฐ ํจ์ ์คํ
let num = 10
if (num > 9) {
resolve(num) //successCallback
} else {
reject("error") //failCallback
}
}, 1000);
}
)
func
.then((item) => { //successCallback
console.log('success-1',item)
return 1
})
.then((num) => {
console.log('success-2',num)
})
.catch((err) => {
console.log(err)
})
.finally(() => {
console.log("Finish")
})
>>>
success-1 10
success-2 1
Finish
์ด๋ฒ์๋ reject๋์์ ๋!
const func = new Promise(
(resolve, reject) => { //์ฑ๊ณต, ์คํจ ํจ์์ธ์๋ก ๋ฐ๊ธฐ
setTimeout(() => { //๋น๋๊ธฐ ํจ์ ์คํ
let num = 10
if (num > 90) {
resolve(num) //successCallback
} else {
reject("error") //failCallback
}
}, 1000);
}
)
func
.then((item) => { //successCallback
console.log('success-1',item)
return 1
})
.then((num) => {
console.log('success-2',num)
})
.catch((err) => {
console.log(err)
})
.finally(() => {
console.log("Finish")
})
>>>
error
Finish
์ด๋ catch์ finally์ ์์๋ฅผ ๋ค๋ฐ๊พธ์ด ๋ณด์
const func = new Promise(
(resolve, reject) => { //์ฑ๊ณต, ์คํจ ํจ์์ธ์๋ก ๋ฐ๊ธฐ
setTimeout(() => { //๋น๋๊ธฐ ํจ์ ์คํ
let num = 10
if (num > 90) {
resolve(num) //successCallback
} else {
reject("error") //failCallback
}
}, 1000);
}
)
func
.then((item) => { //successCallback
console.log('success-1',item)
return 1
})
.then((num) => {
console.log('success-2',num)
})
.finally(() => {
console.log("Finish")
})
.catch((err) => {
console.log(err)
})
>>>
Finish
error
์ด ๊ฒฝ์ฐ์๋
finally๊ฐ ๋จผ์ ์คํ์ด ๋๊ณ
catch๊ฐ ์คํ์ด ๋๋ค.
์ผ๋จ ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด ํ๋ก๊ทธ๋จ์ ์ข ๋ฃ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์
finally๋ฅผ ์คํํ๋ ๊ฒ์ด๋ค.
Promise.all
๋๊ธฐํํ Promise๋ค ๋ชจ๋ ์คํ
๋ค ๋๋ธ ํ .then๊ตฌ๋ฌธ ์คํ
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => resolve("100ms"), 100)
})
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => resolve("500ms"), 500)
})
const promise3 = new Promise((resolve, reject) => {
setTimeout(() => resolve("1500ms"), 1500)
})
Promise.all([promise1, promise2, promise3]) //๋๊ธฐํํ ๊ฒ๋ค์ ๋ชจ๋ ์คํ
.then((data) => { //promise1,2,3์ ์ธ์๊ฐ ๋ค ๋ฐ์์ ๋ฆฌ์คํธ๋ก
console.log(data) //๋ฆฌ์คํธ ์ถ๋ ฅ
})
>>>
[ '100ms', '500ms', '1500ms' ]
Promise.race
์ฌ๋ฌ Promise๋ค ์ค ๊ฐ์ฅ ๋นจ๋ฆฌ ์๋ฃํ ๊ฒ๋ง
.then๊ตฌ๋ฌธ ์คํ
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => resolve("500ms"), 500)
})
const promise2 = new Promise((resolve, reject) => {
setTimeout(() => resolve("100ms"), 100)
})
const promise3 = new Promise((resolve, reject) => {
setTimeout(() => resolve("1500ms"), 1500)
})
// Promise.all([promise1, promise2, promise3])
// .then((data) => {
// console.log(data)
// })
Promise.race([promise1, promise2, promise3])
.then((data) => {
console.log(data)
})
>>>
100ms
๊ฐ์ฅ ๋น ๋ฅธ promise2๋ง .then๊ตฌ๋ฌธ์ด ์คํ๋์ด
100ms๋ง ์ถ๋ ฅ๋์๋ค.
'๐ | WEB DEV > Vanilla JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JS_๋ฌธ๋ฒ (8)_DOM CRUD ํํค์น๊ธฐ (0) | 2022.02.24 |
---|---|
JS_๋ฌธ๋ฒ (7)_DOM / BOM (0) | 2022.02.23 |
JS_๋ฌธ๋ฒ (5)_๋๊ธฐ/๋น๋๊ธฐ ์ฒ๋ฆฌ (0) | 2022.02.18 |
JS_๋ฌธ๋ฒ (4)_Scope (0) | 2022.02.17 |
JS_๋ฌธ๋ฒ (3)_ํธ์ด์คํ (0) | 2022.02.16 |
๋๊ธ