生成器
function* gen() {
yield Promise.resolve('小满')
yield '大满'
yield '塞满了'
}
const man = gen()
console.log(man.next())
console.log(man.next())
console.log(man.next())
console.log(man.next())
迭代器
const each = (value: any) => {
let It: any = value[Symbol.iterator]()
let next: any = { done: false }
while (!next.done) {
next = It.next()
if (!next.done) {
console.log(next.value)
}
}
}
each(set)
let obj1 = {
max: 5,
current: 0,
[Symbol.iterator]() {
return {
max: this.max,
current: this.current,
next() {
if (this.current === this.max) {
return { value: undefined, done: true }
} else {
return { value: this.current++, done: false }
}
}
}
}
}