README
from tj/co
but lesser
install
npm install shy-co
version 0.1.2
it's stable
How to Use
'use strict'
let co=reuqire('shy-co')
co(function*(){
let x
x=yield [666,Promise.resolve(6)]
console.log(x)//log:[666,6]
x=yield Promise.resolve(6)
console.log(x)//log:6
x=yield ()=>7
console.log(x)//log 7
x=yield 666
console.log(x)//log: 666 (ps:return self)
x=yield Promise.reject(6)
console.log(x)//not run
}).then(
null,
err=>console.log(err)//log:6
)
Advantage
you can yield
anything
if you yield x
,it will do yield co(x)
ps:Promise>[]>function*>function>value
ES3
//实例
var co = require('shy-co/es3.js')//兼容ie8,但是用法不一致
co(function gen(yield){//名字含有gen的函数会得到一个taskAdd函数作为参数
yield(function(){
return 0
})
yield(function Promise(val,rj,rj){//名字含有promise的函数会多出两个参数promise构造函数中的resolve,reject
console.log(val)//log:0
rl(1)
})
yield(function(val){
console.log(val)//log:1
throw 2
},function err(err){//这个函数会被添加到promise中的catch参数中,没有的话错误直接抛出
console.log(val)//log:2
return 2
})
})
update
0.1.7 ~ 0.1.10 bug修复,某些情况下错误不被抛出
0.1.6 进行一些优化
0.1.5
增加ie8直接使用,不过写法不一样,依赖native-promise-only
包
0.1.4 BUG修复
0.1.3 BUG修复,内部错误忘记抛出
0.1.2 let promisify = require('shy-co/promisify')