ES6-24 生成器与迭代器的应用

手写生成器

  • 先done再false,不然index就提前了一步+1
var arr = [1,2]
function generator(arr){var i = 0;return{next(){var done = i > arr.length ? true : false,value = done ? 'undefined' : arr[i++];return {value : value,done : done}  }}
}
var gen = generator(arr);
console.log(gen.next());
console.log(gen.next());
console.log(gen.next());

应用一

  • n个函数,点一次执行一个

顺序执行

  • 用return false来中断执行
var fn = [function test1(){console.log(1);return true},function test2(){console.log(2);return false},function test3(){console.log(3);return true}
]
for(var i of fn){if(!i()){break}
}

中间件

  • node express 洋葱模型
  • 表单校验?获取验证码+登录+请求?获取token+校验token+打开页面?
  • 递归 + 函数用next()控制下一次执行
  • 用done控制传入数组内方法的执行
  • 在每个方法里按具体业务控制next的执行
;(function(functions){function * doFun(arr){for(var i = 0; i < arr.length; i++){yield arr[i];}}var iterator = doFun(functions);var init=()=>{nextDo(iterator.next())}function nextDo(fn){fn.value(function(){var fn = iterator.next();if(!fn.done){nextDo(fn)}else{return}})}init()})
([function test1(next){console.log(1);next()  },function test2(next){console.log(2);next()  },function test3(next){console.log(3);next()  }]
)

模块化

// middleware.js
export const M = function(functions){function * doFun(arr){for(var i = 0; i < arr.length; i++){yield arr[i];}}var iterator = doFun(functions);var init=()=>{nextDo(iterator.next())}function nextDo(fn){fn.value(function(){var fn = iterator.next();if(!fn.done){nextDo(fn)}else{return}})}init()}
M([function test1(next){console.log(1);next()  },function test2(next){console.log(2);// 伪代码// if(token){//	next()// }next()  },function test3(next){console.log(3);next()  }]
)

在这里插入图片描述

打印日志

  • TJ co/test/generator
var assert = require('assert');
var co = require('..');function sleep(ms) {return function(done){setTimeout(done, ms);}
}function *work() {yield sleep(50);return 'yay';
}describe('co(*)', function(){describe('with a generator function', function(){it('should wrap with co()', function(){return co(function *(){var a = yield work;var b = yield work;var c = yield work;assert('yay' == a);assert('yay' == b);assert('yay' == c);var res = yield [work, work, work];assert.deepEqual(['yay', 'yay', 'yay'], res);});})it('should catch errors', function(){return co(function *(){yield function *(){throw new Error('boom');};}).then(function () {throw new Error('wtf')}, function (err) {assert(err);assert(err.message == 'boom');});})})
})

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/250960.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1013 B. And

链接 [http://codeforces.com/contest/1013/problem/B] 题意 给你一个n和x,再给n个数&#xff0c;有一种操作用x&a[i]取代&#xff0c;a[i],问使其中至少两个数相同&#xff0c;要多少次操作&#xff0c;如果不能输出-1. 思路 x&a[i],无论&多少次&#xff0c;a[i]都…

jquery --- 收缩兄弟元素

点击高亮的收缩兄弟元素. 思路: 1.点击的其实是tr.(类为parent) 2.toggleClass可以切换样式 3.slblings(’.class’).toggle 可以根据其类来进行隐藏显示 代码如下: <!DOCTYPE html> <html> <head> <meta charset"utf-8"><style>.pa…

Webpack基础

path.resolve // 只要以/开头&#xff0c;就变为绝对路径 // ./和直接写效果相同 var path require("path") //引入node的path模块path.resolve(/foo/bar, ./baz) // returns /foo/bar/baz path.resolve(/foo/bar, baz) // returns /foo/bar/baz path.res…

(php)实现万年历

1 <?php2 //修改页面编码3 header("content-type:text/html;charsetutf-8");4 5 //获取当前年6 $year$_GET[y]?$_GET[y]:date(Y);7 8 //获取当年月9 $month$_GET[m]?$_GET[m]:date(m); 10 11 //获取当前月多少天 12 $daysdate(t,strtotime("{$year}-{$m…

LeetCode:二叉树相关应用

LeetCode&#xff1a;二叉树相关应用 基础知识 617.归并两个二叉树 题目 Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new …

ubuntu16.04 python3.5 opencv的安装与卸载(转载)

转载https://blog.csdn.net/qq_37541097/article/details/79045595 Ubuntu16.04 自带python2.7和python3.5两个版本&#xff0c;默认为python2.7&#xff0c;我使用的是3.5&#xff0c;所以首先将默认的python版本改为3.5. 在终端输入下列指令&#xff1a; sudo update-alterna…

Webpack进阶(一) tree shaking与不同mode

Tree Shaking 生产环境去除没有使用到的内容&#xff08;开发环境没有删除&#xff0c;会影响调试&#xff09;只支持ESM规范&#xff08;静态引入&#xff0c;编译时引入&#xff09;&#xff0c;不支持CJS&#xff08;动态引入&#xff0c;执行时引入&#xff09; // webpa…

jquery --- 网页选项卡

点击,不同的tab_menu,显示不同的tab_box 注意点: 1.获取ul下,当前li的编号. $(‘div ul li’).index(this) 2.显示ul下编号为$index的li -> $(‘ul li’).eq($index) <!DOCTYPE html> <html> <head> <meta charset"utf-8"> <style&g…

Webpack进阶(二)代码分割 Code Splitting

源代码index.js里包含2部分① 业务逻辑代码 1mb② 引入&#xff08;如lodash包&#xff09;的代码 1mb若更新了业务逻辑代码&#xff0c;但在浏览器运行时每次都下载2mb的index.js显然不合理&#xff0c;第三方包是不会变的 手动拆分 webpack.base.js entry: {main: path.re…

5177. 【NOIP2017提高组模拟6.28】TRAVEL (Standard IO)

Description Input Output Solution 有大佬说&#xff1a;可以用LCT做。&#xff08;会吗&#xff1f;不会&#xff09; 对于蒟蒻的我&#xff0c;只好用水法&#xff08;3s&#xff0c;不虚&#xff09;。 首先选出的泡泡怪一定是连续的一段 L&#xff0c; R 然后 L 一定属于虫…

python 3.x 爬虫基础---http headers详解

python 3.x 爬虫基础 python 3.x 爬虫基础---http headers详解 python 3.x 爬虫基础---Urllib详解 python 3.x 爬虫基础---Requersts,BeautifulSoup4&#xff08;bs4&#xff09; python 3.x 爬虫基础---正则表达式 前言  上一篇文章 python 爬虫入门案例----爬取某站上海租房…

Webpack进阶(三)

懒加载 lazy loading 用到的时候才加载vue 首屏不加载index.js const oBtn document.getElementById(j-button) oBtn.onclick async function () {const div await createElement()document.body.appendChild(div) } async function createElement() {const { default: _ …

P2634 [国家集训队]聪聪可可

链接&#xff1a;https://www.luogu.org/problemnew/show/P2634 题目描述 聪聪和可可是兄弟俩&#xff0c;他们俩经常为了一些琐事打起来&#xff0c;例如家中只剩下最后一根冰棍而两人都想吃、两个人都想玩儿电脑&#xff08;可是他们家只有一台电脑&#xff09;……遇到这种问…

算法 --- 快慢指针判断链表是否有环

解题思路: 分别设置2个指针(s,q)指向链表的头部,s每次指向下面一个(s s.next),q每次指向下面2个(q q.next.next). 如果存在环,q总会在某一时刻追上s /*** Definition for singly-linked list.* function ListNode(val) {* this.val val;* this.next null;* }*//**…

APP拉起小程序

结论&#xff1a;APP可以唤起小程序&#xff0c;前提是APP开发者在微信开放平台帐号下申请移动应用&#xff0c;通过审核并关联小程序&#xff0c;参考如下&#xff1a; 准备工作: APP开发者认证微信开放平台 https://kf.qq.com/faq/170824URbmau170824r2uY7j.html APP开发者…

node --- 使用nrm改变npm的源

说明: 1.nrm只是单纯的提供了几个常用的下载包的URL地址,方便我们再使用npm装包是 很方便的进行切换. 2.nrm提供的cnpm 和通过 cnpm装包是2个不同的东西(使用cnpm install必须先安装cnpm -> npm install -g cnpm) 安装nrm: // linux $ [sudo] npm install --global nrm// w…

MySQL教程(三)—— MySQL的安装与配置

1 安装MySQL 打开附件中的文件&#xff08;分别对应电脑系统为32/64位&#xff09;。点next。 三个选项&#xff0c;分别对应典型安装、自定义安装和完全安装&#xff0c;在此选择典型安装&#xff08;初学者&#xff09;。 点install。 广告&#xff0c;忽略它。 安装完成…

算法面经之百度

一、百度 前言&#xff1a;本来不打算写百度面筋的&#xff0c;因为二面表现自我感觉实在太差了&#xff0c;像是被生活抽了一记耳光&#xff0c;不愿再去揭伤疤&#xff0c;奈何&#xff0c;半个月过去了&#xff0c;昨天又被百度从备胎池拉出来涮了一遍&#xff0c;涮的时候也…

flask-session总结

一、session session和cookie的原理和区别&#xff1a; cookie是保存在浏览器上的键值对 session是存在服务端的键值对&#xff08;服务端的session就是一个大字典&#xff0c;字典中是随机字符串&#xff09;&#xff08;session与request原理相同&#xff09;&am…