async findAddressHandler(e) {
e.preventDefault();
const address = document.querySelector("#address-input").value;
if (!address || address.trim().length === 0) {
alert("Invalid address entered. Please try again.");
return;
}
const modal = new Modal(
"loading-modal-content",
"Loading location - please wait!"
);
modal.show();
// 사용자 입력 주소를 좌표로 변환
const foundCoords = await getCoordsFromAddress(address);
console.log(foundCoords);
}
위 코드에서 getCoordsFromAddress 함수는 리턴값으로 프로미스를 반환한다.
일반 함수처럼 변수에 해당 리턴값을 할당하고, console.log를 찍으면 'fulfilled' 상태의 프로미스가 나타난다.
이 프로미스는 받아온 곳에서 풀어줘야 하는데,
위 코드는 async/await 형식으로 풀어주었기 때문에
(호출하는 함수인 findAddressHandler()에 async를 걸고, 호출된 함수인 getCoordsFromAddress()에 await를 걺)
console.log 찍었을 때 프로미스 안에 숨겨져 있던 실제 반환값이 나온다.
'JS' 카테고리의 다른 글
| debounce & throttle (0) | 2025.01.05 |
|---|---|
| 나의 첫 'REST API' 사용 (0) | 2024.09.16 |
| [webpack] build:dev와 build:prod의 차이 (0) | 2024.09.07 |
| 한 컷으로 보는 개발 워크플로우 (dev-prod) (0) | 2024.09.06 |
| ESLint: 그냥 코드 정리해주는 도구? (0) | 2024.09.06 |