
finally method어떤 작업의 성공 여부와 관계없이, 즉 promise 객체의 상태가 fulfilled가 되어도 rejected 상태가 되어도 상관없이 항상 실행하고 싶은 콜백 함수가 있을 때는 finally method로 등록할 수 있다.
때문에, finally method 안의 콜백은 작업 성공 결과나 작업 실패 정보가 필요하지 않기 때문에, 파라미터는 필요하지 않다.
아래 예시 코드의 경우, finally method를 사용했기 때문에, 어떤 경우가 되어도 exit이라는 문자열이 항상 출력되며, catch method가 리턴하는 promise 객체의 상태가 rejected 상태가 되어도 exit 문자열은 출력된다.
fetch('https://jsonplaceholder.typicode.com/users')
.then((response) => response.text())
.then((result) => console.log(result);)
.catch((error) => {console.log(error);})
.finally(() => {console.log('exit');})
일반적으로, finally method는 catch method 바로 뒤에 작성한다는 것도 기억해두자.