什么是 JavaScript 中的异步方法?
顾名思义,异步函数声明定义了一个异步函数。此函数会返回 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>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP