nodejs的co简单实现

2016-12-08

Generator和yield

es6有一个generator函数,它是一个普通函数,但是有两个特征。一是,function关键字与函数名之间有一个星号;二是,函数体内部使用yield语句,定义不同的内部状态

每次执行generator函数会返回一个iterator,在有yield的关键字停止,所以每次执行一次iterator.next会在下一个yield停止,可以通过 next(val) 把结果返回出来

根据这个原理来实现一个简单的co函数

function co (gen) {
    var g = gen();
    var next = g.next();
    var res = next.value;
    function _next (next, res) {
        if (!next.done) {
            if (res && res.constructor == Promise) {
                res.then(function(val) {
                    next = g.next(val);
                    res = next.value;
                    _next(next, res);
                })
            } else if (res.constructor == Function) {
                var fn = res;
                fn (function (err, res) {
                    next = g.next(res);
                    res = next.value;
                    _next(next, res);
                })
            } else {
                next = g.next(res);
                res = next.value;
                _next(next, res);
            }
        }
    }
    _next(next, res);
}

测试一下~

var fs = require("fs");
var readThunk = thunkify(fs.readFile);

co(function * gen() {
    var pro = new Promise(function(resolve, reject) {
        resolve(1999);
    });
    var r3=  yield pro;
    var r1 = yield 100;
    var r2 = yield 200;
    var a = yield readThunk("./a.html");
    var c = yield function (fn) {
        setTimeout(function() {
            fn(null, 188);
        }, 1000)
    }
    console.log(r3, r1, r2, a);
    console.log(c);
})

// 结果
1999 100 200 <Buffer 0d 0a 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0d 0a 3c 68 74 6d 6c 20 6c 61 6e 67 3d 22 65 6e 22 3e 0d 0a 3c 68 65 61 64 3e 0d 0a 20 20 20 20 3c ... >
188

 

本文为原创,未经授权,禁止任何媒体或个人自媒体转载
商业侵权必究,如需授权请联系340443366@qq.com
标签: nodejs

评论

Loading...

最新评论

相关推荐

nginx常用配置
nginx是什么 Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。可以管...
理解v8的内存管理
V8的内存管理模式 一个运行的程序通常是通过在内存中分配一部分空间来表示的。这部分空间被称为驻留集(Resident ...
vue2+vuex+vue-router+webpack+es6搭建项目架构
vue全家桶包括vue,vue-router,vuex等模块,vue负责渲染,vue-route负责路由,vuex负责状...
scroll-top-icon