๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒŒ | WEB DEV/Vanilla JS

JS_๋ฌธ๋ฒ• (6)_promise ๋ฉ”์„œ๋“œ(catch, finally, all, race ๋“ฑ )

by KASSID 2022. 2. 20.

๋ชฉ์ฐจ

    728x90

    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๋งŒ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค.

    ๋Œ“๊ธ€