ES6 Generator基本使用方法示例
本文實例講述了ES6 Generator基本使用方法。分享給大家供大家參考,具體如下:
1.Generator介紹
先來一段Generator的基礎代碼
function* g(){ yield 100; yield 200; return 300; } let gg = g(); console.log(gg); // Object [Generator] {} console.log(gg.next()); // { value: 100, done: false } console.log(gg.next()); // { value: 200, done: false } console.log(gg.next()); // { value: 300, done: true } console.log(gg.next()); // { value: undefined, done: true }
首先我們看到:
- Generator是由functinon*定義的,在generator內(nèi)部可以使用yield
- Generator不是函數(shù),而是一個對象,并且在執(zhí)行開始就進入暫停狀態(tài),而不是直接執(zhí)行全部操作
- 通過next()來執(zhí)行下一步操作,返回的都是{ value: xxx, done: xxx }這樣的形式,value代表上一次操作返回的值,done有兩個值,一個是true,一個是false,表示整個流程是否全部結束。
2.Generator異步編程
generator是ES6中引入的異步解決方案,我們來看看它與promise處理異步的對比,來看它們的差異。
// 這里模擬了一個異步操作 function asyncFunc(data) { return new Promise( resolve => { setTimeout( function() { resolve(data) },1000 ) }) }
promise的處理方式:
asyncFunc("abc").then( res => { console.log(res); // "abc" return asyncFunc("def") }).then( res => { console.log(res); // "def" return asyncFunc("ghi") }).then( res => { console.log(res); // "ghi" })
generator的處理方式:
function* g() { const r1 = yield asyncFunc("abc"); console.log(r1); // "abc" const r2 = yield asyncFunc("def"); console.log(r2); // "def" const r3 = yield asyncFunc("ghi"); console.log(r3); // "ghi" } let gg = g(); let r1 = gg.next(); r1.value.then(res => { let r2 = gg.next(res); r2.value.then(res => { let r3 = gg.next(res); r3.value.then(res => { gg.next(res) }) }) })
promise多次回調(diào)顯得比較復雜,代碼也不夠簡潔,generator在異步處理上看似同步的代碼,實際是異步的操作,唯一就是在處理上會相對復雜,如果只進行一次異步操作,generator更合適。
3.yield和yield*
先來看兩段代碼
function* g1() { yield 100; yield g2(); return 400; } function* g2() { yield 200; yield 300; } let gg = g1(); console.log(gg.next()); // { value: 100, done: false } console.log(gg.next()); // { value: Object [Generator] {}, done: false } console.log(gg.next()); // { value: 400, done: true } console.log(gg.next()); // { value: undefined, done: true }
function* g1() { yield 100; yield* g2(); return 400; } function* g2() { yield 200; yield 300; } let gg = g1(); console.log(gg.next()); // { value: 100, done: false } console.log(gg.next()); // { value: 200, done: false } console.log(gg.next()); // { value: 300, done: false } console.log(gg.next()); // { value: 400, done: true }
yield對另一個generator不會進行遍歷,返回的是迭代器對象,而yield*會對generator進行遍歷迭代。
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
更多關于JavaScript相關內(nèi)容感興趣的讀者可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯誤與調(diào)試技巧總結》、《JavaScript數(shù)據(jù)結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數(shù)學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。
相關文章
js中使用replace方法完成某個字符的轉(zhuǎn)換
這篇文章主要介紹了js中使用replace方法完成某個字符的轉(zhuǎn)換,比較實用,需要的朋友可以參考下2014-08-08解決ueditor jquery javascript 取值問題
這篇文章主要介紹了解決ueditor jquery javascript 取值問題,需要的朋友可以參考下2014-12-12electron-builder書寫nsis腳本在安裝electron程序時安裝其他應用程序(完美解決方案)
這篇文章主要介紹了electron-builder書寫nsis腳本在安裝electron程序時安裝其他應用程序的解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03