# Generator（三）

### Iterator高级功能

#### 给迭代器传递参数

``````function *createIterator() {
let first = yield 1;
let second = yield first + 2; // 4 + 2
yield second + 3; // 5 + 3
}
let iterator = createIterator();
console.log(iterator.next()); // "{ value: 1, done: false }"
console.log(iterator.next(4)); // "{ value: 6, done: false }"
console.log(iterator.next(5)); // "{ value: 8, done: false }"
console.log(iterator.next()); // "{ value: undefined, done: true }"
``````

1，输出1，然后停止执行；第二次调用next(4)，参数为4，继续生成器的执行，4作为yield 1的值被赋给first，然后执行到yield first + 2，输出6，停止执行。整个执行过程如下图所示：

#### 在迭代器中抛出错误

``````function *createIterator() {
let first = yield 1;
let second = yield first + 2; // yield 4 + 2, then throw
yield second + 3; // never is executed
}
let iterator = createIterator();
console.log(iterator.next()); // "{ value: 1, done: false }"
console.log(iterator.next(4)); // "{ value: 6, done: false }"
console.log(iterator.throw(new Error("Boom"))); // error thrown from generator
``````

``````function *createIterator() {
let first = yield 1;
let second;
try {
second = yield first + 2; // yield 4 + 2, then throw
} catch (ex) {
second = 6; // on error, assign a different value
}
yield second + 3;
}
let iterator = createIterator();
console.log(iterator.next()); // "{ value: 1, done: false }"
console.log(iterator.next(4)); // "{ value: 6, done: false }"
console.log(iterator.throw(new Error("Boom"))); // "{ value: 9, done: false }"
console.log(iterator.next()); // "{ value: undefined, done: true }"
``````

#### 生成器返回语句

``````function *createIterator() {
yield 1;
return;
yield 2;
yield 3;
}
let iterator = createIterator();
console.log(iterator.next());           // "{ value: 1, done: false }"
console.log(iterator.next());           // "{ value: undefined, done: true }"
``````

``````function *createIterator() {
yield 1;
return 42;
}
let iterator = createIterator();
console.log(iterator.next());           // "{ value: 1, done: false }"
console.log(iterator.next());           // "{ value: 42, done: true }"
console.log(iterator.next());           // "{ value: undefined, done: true }"
``````

#### 委托生成器(Delegating Generators)

``````function *createNumberIterator() {
yield 1;
yield 2;
}
function *createColorIterator() {
yield "red";
yield "green";
}
function *createCombinedIterator() {
yield *createNumberIterator();
yield *createColorIterator();
yield true;
}
var iterator = createCombinedIterator();
console.log(iterator.next());           // "{ value: 1, done: false }"
console.log(iterator.next());           // "{ value: 2, done: false }"
console.log(iterator.next());           // "{ value: "red", done: false }"
console.log(iterator.next());           // "{ value: "green", done: false }"
console.log(iterator.next());           // "{ value: true, done: false }"
console.log(iterator.next());           // "{ value: undefined, done: true }"
``````

``````function *createNumberIterator() {
yield 1;
yield 2;
return 3;
}
function *createRepeatingIterator(count) {
for (let i=0; i < count; i++) {
yield "repeat";
}
}
function *createCombinedIterator() {
let result = yield *createNumberIterator();
yield *createRepeatingIterator(result);
}
var iterator = createCombinedIterator();
console.log(iterator.next());           // "{ value: 1, done: false }"
console.log(iterator.next());           // "{ value: 2, done: false }"
console.log(iterator.next());           // "{ value: "repeat", done: false }"
console.log(iterator.next());           // "{ value: "repeat", done: false }"
console.log(iterator.next());           // "{ value: "repeat", done: false }"
console.log(iterator.next());           // "{ value: undefined, done: true }"
``````

### 异步任务运行

``````let fs = require("fs");
if (err) {
throw err;
}
doSomethingWith(contents);
console.log("Done");
});
``````

#### 简单的任务运行器

``````function run(taskDef) {
// create the iterator, make available elsewhere
// recursive function to keep calling next()
function step() {
// if there's more to do
if (!result.done) {
step();
}
}
// start the process
step();
}
run(function*() {
console.log(1);
yield;
console.log(2);
yield;
console.log(3);
});
``````

#### 带数据的任务运行器

``````function run(taskDef) {
// create the iterator, make available elsewhere
// recursive function to keep calling next()
function step() {
// if there's more to do
if (!result.done) {
step();
}
}
// start the process
step();
}
run(function*() {
let value = yield 1;
console.log(value);         // 1
value = yield value + 3;
console.log(value);         // 4
});
``````

#### 异步任务运行器

``````let fs = require("fs");

// create the iterator, make available elsewhere
// recursive function to keep calling next()
function step() {
// if there's more to do
if (!result.done) {
if (typeof result.value === "function") {
result.value(function(err, data) {
if (err) {
return;
}
step();
});
} else {
step();
}
}
}
// start the process
step();
}