<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
function muti (n) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(n * n)
}, 1000);
})
}
const arr = [1, 2, 3]
// 同步循环,1秒之后同时打印出数组中的数据
arr.forEach(async (i) => {
console.log(await muti(i));
})
// 异步调用,每个都有结果之后再进行下次循环,即每隔1秒打印出一个数据
!(async function () {
for (let i of arr) {
const res = await muti(i)
console.log(res);
}
})()
</script>
</html>