7.如何处理循环的异步操作

function getMoney(){
    var money=[100,200,300]  
    for( let i=0; i<money.length; i++){
        compute.exec().then(()=>{
            console.log(money[i])
            //alert(i)
        })
    }
}
//compute.exec()这是个异步方法,在里面处理一些实际业务

正确处理思路

async function getMoney(){
    var money=[100,200,300]  
    for( let i=0; i<money.length; i++){
        await compute.exec().then(()=>{
            console.log(money[i])
            //alert(i)
        })
    }
}

 

posted @ 2023-03-04 16:03  不想做混子的奋斗远  阅读(31)  评论(0)    收藏  举报