[Javascript] Delegate JavaScript (ES6) generator iteration control

We can execute generators from generators, and delegate the iteration control with the yield* keyword.

Yo dawg, I heard you like generators, so I put generators inside your generators.

 

 
复制代码
function* create3To4Counter() {
    yield 3;
    return 4;
}

function* createCounter() {
    yield 1;
    yield 2;
    const four = yield* create3To4Counter();
    console.log("four", four)
    yield 5;
}

for (let i of createCounter()) {
    console.log(i)
}

/*
1
2
3
4
5
*/
复制代码

 

posted @   Zhentiw  阅读(163)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-02-06 [React] Use React.cloneElement to Extend Functionality of Children Components
2017-02-06 [React] Use React ref to Get a Reference to Specific Components
2017-02-06 [React] Normalize Events with Reacts Synthetic Event System
2017-02-06 [NPM] Pipe data from one npm script to another
2017-02-06 [NPM] Pass arguments to npm scripts
2017-02-06 [Vue] Get up and running with vue-router
2017-02-06 [Node.js] Identify memory leaks with nodejs-dashboard
点击右上角即可分享
微信分享提示