1.forEach只能处理同步的代码
希望4,9,16每隔一秒显示,使用forEach实际为同时显示
<script>function muti(num){return new Promise((resolve,reject)=>{setTimeout(()=>{resolve(num*num)},1000)})}var arr = [2,3,4];(async()=>{arr.forEach(async(item)=>{ //forEach只能处理同步的代码,是一个同步的循环var res = await muti(item)console.log(res)})})();</script>
2.for…of方法
支持异步循环
实现效果:4,9,16每隔一秒显示
function muti(num){return new Promise((resolve,reject)=>{setTimeout(()=>{resolve(num*num)},1000)})}var arr= [2,3,4];(async()=>{for(let value of arr){let res = await muti(value)console.log(res)}})();
WeChatDevelopment
HTML/CSS
web配置项
关注
