Я хотел бы знать, как, если возможно, переписать метод next()
пользовательского итератора.Например, у меня есть функция, которая возвращает итератор:
function myFunction(...args) {
return {
async next() {
// Lazy!
const id = await resource.getId(...args)
console.log('first')
// After first call, overwrite the next!
this.next = async () => {
console.log('second')
// with id, do other thing
const value = await resource2.getOtherThing(id)
if (value.property) return { done: false, value }
return { done: true, value }
}
return this.next()
}
[Symbol.asyncIterator] {
return this
}
}
}
Но это на самом деле не перезаписывается, потому что first
появляется в каждом цикле:
for await (const x of myFunction(...) {
console.log(x)
}
СпасибоВы заранее.