본문 바로가기

JavaScript/Web Development

[JavaScript] finally 메소드

[finally 메소드]

어떤 작업의 성공 유무 (fulfilled, rejected) 에 상관없이, 항상 실행하고 싶은 콜백이 있을 때 사용하는 메소드

 

fetch('https://jsonplaceholder.typicode.com/users')
    .then((response) => response.text())
    .then((result) => { console.log(result); })
    .catch((error) => { console.log(error); })
    .finally(() => { console.log('exit'); });

 

어떤 경우에도 exit 이 출력되며,

.finally 메소드 안의 콜백은 작업성공 결과, 작업실패 정보가 필요 없어서 파라미터가 필요하지 않음.

 

위와 같은 결과가 출력됨.

 

promise 객체가 rejected 인 경우 (잘못된 url 주소 입력), 

 

exit 는 정상 출력됨.