HTML,CSS, JS/JavaScript

[JavaScript] 짐코딩 - (4) Promise 체이닝이란?

Song hyun 2024. 11. 6. 15:17
728x90
반응형

[JavaScript] 짐코딩 - (4) Promise 체이닝이란?

 

1. Promise 체이닝이란?

-> Promise 체이닝은 promise 객체로 메서드 체이닝을 하는 것

-> 체이닝(Chaining)은 연속적으로 함수를 호출하는 것!

-> 콜백 지옥을 해결할 수 있는 방법!

 

 

2. 시나리오 코드

fetch('https://jsonplaceholder.typicode.com/todos?_limit=5')
    .then(response=>{
        return response.json()
    })
    .then(data=>{
        console.log('data: ',data)
        return data.filter(obj=>obj.id>3)
        // id가 3 이상인 결과만 출력되도록 필터링해준다.
    })
    .then(result=>{
        console.log('result: ',result)
    })
    .catch(err=>{
        console.log('err: ',err)
    })
    .finally(()=>{
        console.log('### finally ###')
    })

 

https://inf.run/B3fkQ

 

학습 페이지

 

www.inflearn.com

 

728x90
반응형