javascript - promise

꾸준히 공부·2023년 3월 5일

promise란 javascript에서 비동기처리에 사용되는 객체

fetch 함수는 promise 리턴
fetch.then -> 결과값
fetch.catch -> 실패시 이유

ex )

fetch('url')
	.then(function(response){
    	console.log('response', response);  
        //여기서 response는 response.json(), response.text() 등 
        //타입정할수 있음
        })
        .catch(function(reason){
         
        })
        
        
fetch('url')
	.then(function(response){
       	response.json().then(function(data){
			console.log('data', data);
		});
    })
    .catch(function(reason){
        
    })


 fetch('url')
 	.then(function(response){
     	response.json().then(function(data){
		    return response.json();
		});
    })
    .catch(function(reason){
         
    })
    .then(function(data){
       	console.log('data', data);
    })     -> promise chaining 방식 (일반적인 방식)
        
profile
안녕하세요

0개의 댓글