ES6 Generator基本使用方法示例
本文實(shí)例講述了ES6 Generator基本使用方法。分享給大家供大家參考,具體如下:
1.Generator介紹
先來一段Generator的基礎(chǔ)代碼
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ù),而是一個(gè)對(duì)象,并且在執(zhí)行開始就進(jìn)入暫停狀態(tài),而不是直接執(zhí)行全部操作
- 通過next()來執(zhí)行下一步操作,返回的都是{ value: xxx, done: xxx }這樣的形式,value代表上一次操作返回的值,done有兩個(gè)值,一個(gè)是true,一個(gè)是false,表示整個(gè)流程是否全部結(jié)束。
2.Generator異步編程
generator是ES6中引入的異步解決方案,我們來看看它與promise處理異步的對(duì)比,來看它們的差異。
// 這里模擬了一個(gè)異步操作
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)顯得比較復(fù)雜,代碼也不夠簡(jiǎn)潔,generator在異步處理上看似同步的代碼,實(shí)際是異步的操作,唯一就是在處理上會(huì)相對(duì)復(fù)雜,如果只進(jìn)行一次異步操作,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對(duì)另一個(gè)generator不會(huì)進(jìn)行遍歷,返回的是迭代器對(duì)象,而yield*會(huì)對(duì)generator進(jìn)行遍歷迭代。
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- 詳解JavaScript ES6中的Generator
- Es6 Generator函數(shù)詳細(xì)解析
- JavaScript中 ES6 generator數(shù)據(jù)類型詳解
- ES6新特性三: Generator(生成器)函數(shù)詳解
- ES6中Generator與異步操作實(shí)例分析
- 詳談ES6中的迭代器(Iterator)和生成器(Generator)
- ES6 系列之 Generator 的自動(dòng)執(zhí)行的方法示例
- ES6 Generator函數(shù)的應(yīng)用實(shí)例分析
- ES6中的迭代器、Generator函數(shù)及Generator函數(shù)的異步操作方法
相關(guān)文章
網(wǎng)絡(luò)之美 JavaScript中Get和Set訪問器的實(shí)現(xiàn)代碼
前兩天IE9 Beta版發(fā)布了,對(duì)于從事Web開發(fā)的朋友們來說真是個(gè)好消息啊,希望將來有一天各個(gè)瀏覽器都能遵循統(tǒng)一的標(biāo)準(zhǔn)。今天要和大家分享的是JavaScript中的Get和Set訪問器,和C#中的訪問器非常相似。2010-09-09
TypeScript中的類型斷言[as語(yǔ)法|<>語(yǔ)法]的使用
本文主要介紹了TypeScript中的類型斷言[as語(yǔ)法|<>語(yǔ)法]的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
xmlplus組件設(shè)計(jì)系列之文本框(TextBox)(3)
xmlplus 是一個(gè)JavaScript框架,用于快速開發(fā)前后端項(xiàng)目。這篇文章主要介紹了xmlplus組件設(shè)計(jì)系列之文本框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
layui實(shí)現(xiàn)二維碼彈窗、并下載到本地的方法
今天小編就為大家分享一篇layui實(shí)現(xiàn)二維碼彈窗、并下載到本地的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
js實(shí)現(xiàn)String.Fomat的實(shí)例代碼
下面小編就為大家?guī)硪黄猨s實(shí)現(xiàn)String.Fomat的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
JavaScript 如何刪除小數(shù)點(diǎn)后的數(shù)字
這篇文章主要介紹了JavaScript 刪除小數(shù)點(diǎn)后的數(shù)字實(shí)例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07

