本篇文章為大家展示了ES6中Generator的使用方法,代碼簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)專注于靈山企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),商城建設(shè)。靈山網(wǎng)站建設(shè)公司,為靈山等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站建設(shè),專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
先來一段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ù),而是一個對象,并且在執(zhí)行開始就進(jìn)入暫停狀態(tài),而不是直接執(zhí)行全部操作
通過next()來執(zhí)行下一步操作,返回的都是{ value: xxx, done: xxx }這樣的形式,value代表上一次操作返回的值,done有兩個值,一個是true,一個是false,表示整個流程是否全部結(jié)束。
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)顯得比較復(fù)雜,代碼也不夠簡潔,generator在異步處理上看似同步的代碼,實際是異步的操作,唯一就是在處理上會相對復(fù)雜,如果只進(jìn)行一次異步操作,generator更合適。
先來看兩段代碼
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不會進(jìn)行遍歷,返回的是迭代器對象,而yield*會對generator進(jìn)行遍歷迭代。
上述內(nèi)容就是ES6中Generator的使用方法,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前名稱:ES6中Generator的使用方法
分享網(wǎng)址:http://www.chinadenli.net/article32/gedipc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、定制網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、ChatGPT、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)