NOTE

fetch() error 처리하기

ssund 2023. 3. 17. 00:10

fetch() promise는 HTTP error에 의해 reject 되지 않는다. 
https://developer.mozilla.org/en-US/docs/Web/API/fetch

fetch(api url)
	.then(( response ) => {
    	if(!response.ok) {
        	throw new Error(`error status: ${response.status}`)
        }
        
        return response.json()
    })
    .then(( data ) => {
    	// 처리 코드
        console.log(data)
    })
    .catch((error) => {
    	// 에러 처리 코드
        console.log(error)
    })