Skip to content

Instantly share code, notes, and snippets.

@belocer

belocer/index.js

Last active Sep 25, 2020
Embed
What would you like to do?
Генераторы
// Generator
function *gen (num = 4) {
for (let i = 0; i < num; i++) {
yield i
}
}
const iter = gen(3)
console.log(iter.next()) // { value: 0, done: false }
console.log(iter.next()) // { value: 1, done: false }
console.log(iter.next()) // { value: 2, done: false }
console.log(iter.next()) // { value: undefined, done: true }
function *gen (num = 4) {
for (let i = 0; i < num; i++) {
try {
yield i
} catch (e) {
console.log('Error', e);
}
}
}
const iter = gen(3)
console.log(iter.next()) // { value: 0, done: false }
console.log(iter.throw('My Error')) // Error My Error
console.log(iter.next()) // { value: 2, done: false }
console.log(iter.next()) // { value: undefined, done: true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment