(100分) 为什么yield和next的数目不匹配
bsnry 2017-10-09 06:05:31 这个例子中的next有什么作用?
代码中只有2个yield, 但是例子中有3个next,
我们看第三个next,也就是:
function getTpl(tpl){
console.log("getTpl----" + tpl);
console.log(tpl);
setTimeout(function(){
iterator.next('hello ${name}')
}, 3000)
}
这个next 有什么作用? 为什么yield和next的数目不匹配? 我所知道的,
next有个作用,可以把自己的参数返回给 yield表达式左边的变量,还能执行代码到下一个yield。
可是数目不匹配啊。第一个next的参数是空的。
// 异步任务模型
function getData(src){
console.log("getData---" + src);
setTimeout(function(){
iterator.next({tpl: 'tpl.html', name: 'fsjohnhuang'}) //第二个
}, 1000)
}
function getTpl(tpl){
console.log("getTpl----" + tpl);
console.log(tpl);
setTimeout(function(){
iterator.next('hello ${name}') //第三个
}, 3000)
}
function render(data, tpl){
console.log("--------render-----")
console.log(data, tpl);
return tpl.replace(/\$\{(\w+)\}/, function(){
return data[arguments[1]] == void 0 ? arguments[0] : data[arguments[1]]
})
}
// 主逻辑
function *getAritcles(src){
console.log('begin')
var data = yield getData(src)
var tpl = yield getTpl(data.tpl)
var res = render(data, tpl)
console.log("------getAritcles-----")
console.log(data);
console.log("-----end-----:res:" + res)
}
var iterator = getAritcles('dummy.json')
iterator.next() //第一个