모든 페이지에서 받을 수 있게 Head 파일을 _app.tsx로 옮긴다. (비 추천)
: 필요없는 페이지에서도 지도를 다운 받기 때문에 비효율적이다.
useEffect를 사용하여 그 안에 scriptf를 생성 한 후 로드함수를 만든다.(doc참고)
export default function KakaoMapPage() {
useEffect(() => {
// 여기서 직접 다운 받고, 다 받을 때 까지 기다려주기
const script = document.createElement("script"); // <script></script>
script.src =
"//dapi.kakao.com/v2/maps/sdk.js?appkey={api키}&autoload=false";
document.head.appendChild(script);
script.onload = () => {
window.kakao.maps.load(function () {
...
);
});
};
}, []);
return (
<div id="map" style={{ width: "500px", height: "400px" }}>
</div>
);
}
const onClickCallback = () => {
const aaa = new XMLHttpRequest();
aaa.open("get", "http://numbersapi.com/random?min=1&max=200");
aaa.send();
aaa.addEventListener("load", (res: any) => {
const num = res.target.response.split("")[0]; // 71
const bbb = new XMLHttpRequest();
bbb.open("get", `http://koreanjson.com/posts/${num}`);
bbb.send();
bbb.addEventListener("load", (res: any) => {
const userId = JSON.parse(res.target.response).UserId;
const ccc = new XMLHttpRequest();
ccc.open("get", `http://koreanjson.com/posts?userId=${userId}`);
ccc.send();
ccc.addEventListener("load", (res: any) => {
console.log("최종 결과값");
console.log(JSON.parse(res.target.response));
});
});
});
};
const onClickPromise = () => {
axios
.get("http://numbersapi.com/random?min=1&max=200")
.then((res) => {
const num = res.data.split("")[0];
return axios.get(`http://koreanjson.com/posts/${num}`);
})
.then((res) => {
const userId = res.data.UserId;
return axios.get(`http://koreanjson.com/posts?userId=${userId}`);
})
.then((res) => {
console.log(res);
});
};
const onClickMsyncAwait = async () => {
const res1 = await axios.get("http://numbersapi.com/random?min=1&max=200");
const num = res1.data.split("")[0];
const res2 = await axios.get(`http://koreanjson.com/posts/${num}`);
const userId = res2.data.UserId;
const res3 = await axios.get(
`http://koreanjson.com/posts?userId=${userId}`
);
console.log(res3);
};
두 개의 queue가 동시에 들어올 경우 MicroTaskQueue를 먼저 비운 뒤 MacroTaskQueue를 비운다.