
콜백 함수는 다른 함수의 인자로 전달되어 특정 이벤트 혹은 작업이 완료된 후에 호출되는 함수를 말한다.
콜백함수는 다른 함수의 인자로 전달되며 해당 함수가 완료된 후에 호출된다.
주로 비동기 작업(ex. API 호출, 파일 읽기 등..)의 결과를 처리하기 위해 사용된다.
const onClickCallback = () => {
const aa = new XMLHttpRequest();
aa.open("get", `http://numbersapi.com/random?min=1&max=200`);
aa.send();
aa.addEventListener("load", (res) => {
console.log(res);
const num = res.target.response.split(" ")[0]; // 랜덤숫자 저장
console.log(num)
// 첫 번째 요청 완료 후에 두 번째 요청 실행
const bb = new XMLHttpRequest();
bb.open("get", `https://koreanjson.com/posts/${num}`);
bb.send();
bb.addEventListener("load", (res) => {
console.log(res);
const userID = JSON.parse(res.target.response).UserId; // 유저ID 저장
console.log(userID)
// 두 번째 요청 완료 후에 세 번째 요청 실행
const cc = new XMLHttpRequest();
cc.open("get", `https://koreanjson.com/posts?userId=${userID}`);
cc.send();
cc.addEventListener("load", (res) => {
console.log(res.target.response);
});
});
});
};
1 -> 2 -> 3번을 순차적으로 요청하고 받아오는 코드다.
XMLHttpRequest 객체를 생성해서 API를 통해 1부터 200까지의 랜덤 숫자를 요청하고 받아오면 콜백함수가 실행되어 랜덤숫자를 저장하게 된다.
1번에서 받은 값을 활용해 다른 API에서 정보를 요청한다. 요청이 완료되면 콜백함수가 실행되어 userId를 저장하는 userID가 만들어지게 된다.
2번에서 요청받은 userID를 활용해 마지막 콜백함수가 실행되며 콘솔을 출력한다.
const myPromise = () => {
fetch(`http://numbersapi.com/random?min=1&max=200`) // 첫 번째 fetch 요청
.then((res) => res.text()) // 응답을 텍스트로 변환
.then((qqq) => {
const num = qqq.split(" ")[0]; // 랜덤 숫자 추출
return fetch(`https://koreanjson.com/posts/${num}`); // 두 번째 fetch 요청
})
.then((res) => res.json()) // JSON으로 변환
.then((qqq) => {
const userId = qqq.UserId; // 작성자 ID 추출
return fetch(`https://koreanjson.com/posts?userId=${userId}`); // 세 번째 fetch 요청
})
.then((res) => res.json()) // JSON으로 변환
.then((qqq) => {
console.log(qqq); // 최종 결과 출력
})
.catch((error) => {
console.error("Error:", error); // 에러 처리
});
};
비동기 코드를 더 간결하고 동기 코드처럼 작성할 수 있게 도와준다.
async 키워드를 함수 앞에 붙이면 해당 함수는 항상 프로미스를 반환하게 된다.
함수 내부에서 await 키워드를 사용할 수 있게 해준다.
await는 프로미스가 처리될 때까지 기다린 후, 완료된 값을 반환한다.
const onClickAsyncAwait = async () => {
const res1 = await fetch(`http://numbersapi.com/random?min=1&max=200`)
const qqq1 = await res1.text()
const num = qqq1.split(" ")[0]; // 랜덤숫자
const res2 = await fetch(`https://koreanjson.com/posts/${num}`)
const qqq2 = await res2.json()
const userId = qqq2.userId; // 랜덤숫자
const res3 = await fetch(`https://koreanjson.com/posts?userId=${userId}`)
const qqq3 = await res3.json()
console.log(qqq3)
}