// 콜백 패턴
function fetchData(callback) {
setTimeout(() => {
callback("데이터 로드 완료");
}, 1000);
}
fetchData((result) => {
console.log(result);
});
// Promise 생성
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("성공!");
// reject(new Error("실패!"));
}, 1000);
});
// Promise 사용
promise
.then(result => console.log(result))
.catch(error => console.error(error))
.finally(() => console.log("완료"));
async function fetchData() {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.error(error);
}
}