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.pswp.cn/news/250960.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/250960.shtml
英文地址,請注明出處:http://en.pswp.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…