ES6之生成器

【ES6之生成器】ES6生成器:一种特殊的函数/异步编程/函数代码分隔符

function getUserData() { setTimeout(() => { const data = 'https://www.it610.com/article/用户数据' iterator.next(data); }, 1000) }function getOrderData() { setTimeout(() => { const data = 'https://www.it610.com/article/订单数据' iterator.next(data); }, 1000) }function getGoodData() { setTimeout(() => { const data = 'https://www.it610.com/article/商品数据' iterator.next(data); }, 1000) }function * gen() { // 第二次调用next()传入的参数作为第一个yield函数执行的返回值 const userData = https://www.it610.com/article/yield getUserData(); console.log(userData); const orderData = yield getOrderData(); console.log(orderData); const goodData = yield getGoodData(); console.log(goodData); }let iterator = gen(); iterator.next(); // (1s后) 用户数据 // (1s后) 订单数据 // (1s后) 商品数据

    推荐阅读