我的代码是这样的
const co = require('co');
function are(){
}
function* abc(){
var a = yield are()
console.log(1)
var b = yield are()
console.log(2)
var c = yield are()
console.log(3)
}
co(abc)
我预期的执行结果,控制台应该打印出 1,2,3
但是在执行的时候,程序却报错了
应该说是我的yield
后面应该跟function
但是上面的demo中我的yield
后面跟的已经是function
了为什么却没有预期的输出呢?
第二种写法:
const co = require('co');
function are1(){
}
function are2(){
}
function are3(){
}
function* abc(){
var a = yield are1
console.log(1)
var b = yield are2
console.log(2)
var c = yield are3
console.log(3)
}
co(abc)
.then(data =>{
console.log(data)
})
.catch(err =>{
console.log(err)
})
co
已经接受一个generator的函数了,上面的这种写法也是没办法在控制台输出1,2,3的
你的
yield
后面跟的不是are
这个函数,而是are
执行后的返回值。其实它等于
yield undefined
,这才是报错的原因。