实现koa中的generator用法
尽管koa2中已经被async/await代替。但我还是想自个儿着写一个koa1中的generator。
一,
写这个之前,先写一个可以现实,express中next用法的函数:
var event=require('events').EventEmitter;
class App extends event{
constructor(){
super();
this.arr=[],this.i=0;
this.on('next',data=>{
let node=this.arr[++this.i];
if (!node) return ;
node(this.next);
})
}
next(){
this.emit('next');
}
use(v){
this.arr.push(v);
}
end(){
this.arr[this.i](this.next.bind(this));
}
}
使用:
var myApp=new App();
myApp.use(next=>{
console.log('step1');
next();
}) myApp.use(next=>{
console.log('step2');
}) myApp.end();
二,
很成功。好了。下面写koa里的实现,就直接写function 了。这样写得快些 - -!。。:
function App(){
let arr=[],i=0;
let self=this;
let next=function(){
if(!arr[++i]) return ;
self.end();
};
this.use=function(fn){
arr.push(fn);
}
this.end=function(){
let g=arr[i](next);
let run=function(){ //run函数执行这个generator;
let result=g.next();
if(result.done) return;
result.value();
run();
}
g?run():0;
}
}
使用:
var a=new App();
a.use(function* (next){
console.log('a1');
yield next; })
a.use(function* (next){
console.log('b1');
yield next;
console.log('b2');
yield next;
})
a.use(function* (next){
console.log('c1');
yield next;
})
a.use(function(){
console.log('c2')
})
a.use(function(){
console.log('c3')
}) a.end();
返回结果:
a1;
b1;
c1;
c2;
b2;
c3;
好的这样大致框架就成功了。其余的可以慢慢进化。。
实现koa中的generator用法的更多相关文章
- JavaScript中的Generator函数
1. 简介 Generator函数时ES6提供的一种异步编程解决方案.Generator语法行为和普通函数完全不同,我们可以把Generator理解为一个包含了多个内部状态的状态机. 执行Genera ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- SQL中distinct的用法
SQL中distinct的用法 1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...
- Oracle 中 decode 函数用法
Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...
- jQuery中Animate进阶用法(一)
jQuery中animate的用法你了解多少呢?如果仅仅是简单的移动位置,显示隐藏,哦!天哪你在浪费资源!因为animate太强大了,你可以有很多意想不到的用法!让我们一起研究一下吧~~ 首先要了解j ...
- [转载]js中return的用法
一.返回控制与函数结果,语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制,无函数结果,语法为:return; 在大多数情况下,为事件处理函 ...
- js中this的用法
经过近几周的模拟面试题,我查询了一些资料,今天就来说说,在js中this的用法吧.方法有四:第一,用作全局变量,第二,用作表该对象,第三,用作构造函数,第四,用作call和applay
- jQuery中eq()方法用法实例
本文实例讲述了jQuery中eq()方法用法.分享给大家供大家参考.具体分析如下: 此方法能够获取匹配元素集上的相应位置索引的元素. 匹配元素集上元素的位置索引是从0开始的. 语法结构: 复制代码 代 ...
随机推荐
- Dell-R730 【Pxe+dhcp+ftp+tftp+Kickstart+CentOs6.6】
IP:10.104.0.101 [root@localhost network-scripts]# cat ifcfg-em1 [root@localhost network-scripts]# ip ...
- <span>什么意思
<span> 在CSS定义中属于一个行内元素,在行内定义一个区域,也就是一行内可以被 <span> 划分成好几个区域,从而实现某种特定效果. <span> 本身没有 ...
- Nginx stream(TCP/UDP)负载均衡
Nginx-1.11.6编译安装 nginx编译安装,(平台:ubuntu 14.04); sudo apt-get install zlib1g-dev sudo apt-get install l ...
- openssh-clients(CentOS 7 自带的SSH客户端)
OpenSSH 有自带的服务端 openssh-server(sshd服务) 和 客户端 openssh-clients(ssh命令),平时使用SSH远程登陆Linux都是在本地Windows上使用P ...
- Attribute value is quoted with " which must be escaped when used within the value 问题解决
访问JSP时,报错:Attribute value is quoted with " which must be escaped when used within the value .相信 ...
- 逃出克隆岛 (codevs 2059)
较普通的走迷宫的题 传送门 :codevs 2059 逃出克隆岛 思路 :BFS 即可 PS :传送门 不必重复使用 #include <iostream> #include < ...
- [DP之树形DP]
树形dp出了应该还是比计数dp要简单的 因为很好可以看出来 常用的是一个F记录子树内的 一个G记录子树外的 还有一种就是有环的做过要用状压搞一下 不说这么多直接上例题 [HAOI2015]T1 经典的 ...
- C# 语言规范_版本5.0 (第20章 附录B_语法)
A. 语法 此附录是主文档中描述的词法和语法以及不安全代码的语法扩展的摘要.这里,各语法产生式是按它们在主文档中出现的顺序列出的. A.1 词法文法 input: input-sectionopt i ...
- 在tableview的headerView中添加webView,webView自适应高度
最近在项目中需要添加一个webView加载的页面,下面显示的是对这个webView所显示的内容的一个评论列表 ,列表要根据后台加载过来的HTML自适应的变化高度,tableview的cell在webV ...
- tableviewcell 点击 设置
table?.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //设置cell 下边线 位置 table?.se ...