이름에서 알 수 있듯이 비동기 함수 선언은 비동기 함수를 정의합니다. 이 함수는 AsyncFunction 객체를 반환합니다.
구문
구문은 다음과 같습니다 -
async function functionname([param[, param[, ... param]]]) {
statements to be executed
} 예
5초 후에 결과를 출력하는 예를 봅시다 -
<html>
<body>
<script>
function displayFunction(num) {
return new Promise(resolve => {
setTimeout(() => {
resolve(num);
}, 5000);
});
}
async function add2(num) {
const x = displayFunction(7);
const y = displayFunction(5);
return num * await x * await y;
}
add2(15).then(result => {
document.write("Multiplication Result (after 5 seconds): "+result);
});
</script>
</body>
</html>