async - await (then 대신 사용)
: 기존 fetch - .then 사용하는 대신 더 간편하게 response 값을 가져올 수 있다.
<button id = "show">댓글 조회</button>
<script>
function fetchGet() {
const result = fetch('<http://localhost:8383/api/v1/replies/101>');
//console.log(result);
const json = result.then(res => res.json());
//console.log(json);
const data = json.then(data => {
console.log('then 내부 : ', data); // 내부에서만 배열로 댓글 목록 result 결과 볼 수 있음
return '하하호호';
});
console.log('then 외부 : ', data);
// 💖원래 사용했던 fetch - .then 대신 간단한 코드💖
// .then(res => res.json())
// .then(json => {
// console.log(json);
// });
}
**// 📌 async 함수는 기본적으로 Promise를 소비하는 함수 - then 대신 await가 바로 처리해줌 !
async function fetchGet2() {
// awail : then 메서드를 대신 호출해서 PrimiseResult를 바로 가져옴 !!
const result = await fetch('<http://localhost:8383/api/v1/replies/101>');
//console.log(result); // promise가 아니라 바로 response가 나옴 !
const json = await result.json(); // 바로 댓글 배열 나옴 !
console.log(json);
}
document.getElementById('show').onclick = e => {
// fetchGet();
fetchGet2();
};**
</script>