js for循环的同步代码

async function fn() {    
    for(var i=0; i<5; i++) {
        await (function(){
            return new Promise(function(res, rej){
                setTimeout(function(){
                    console.log("res")
                    res();
                },2000)
            })
        }())
        console.log(i)
    }
};

fn()

 

async function fn() {for(var i=0; i<5; i++) {await (function(){return new Promise(function(res, rej){setTimeout(function(){console.log("res")res();},2000)})}())console.log(i)}};
fn()

posted @ 2020-12-15 22:40  方方和圆圆  阅读(4902)  评论(0编辑  收藏  举报

再过一百年, 我会在哪里?