生成器解决异步操作

46 阅读1分钟
function one(){
    setTimeout({
        console.log(111);
        iterator.next();
    },1000)
}

function two(){
    setTimeout({
        console.log(222);
        iterator.next();
    },2000)
}

function three(){
    setTimeout({
        console.log(333);
        iterator.next();
    },3000)
}

    function * gen(){
        yield one();
        yield two();
        yield three();
    }

let iterator=gen();
iterator.next();