抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

generator真好玩,就是没啥用。。

// generator实现斐波那契

function* fibonacci() {
    let [a, b] = [0, 1];
    while (true) {
        yield b;
        [a, b] = [b, a + b];
    }
}


let gen = fibonacci();

function sleep(sleepTime) {
    setTimeout(() => {
        it.next()
    }, sleepTime)
}


function* main() {
    while (true) {
        console.log(gen.next());
        yield sleep(1000)
    }
}


const it = main()

it.next()

评论