17. その逆も出来る
generator内部の状態を知るだけでなく、next()メソッドで
generator内部の状態を操作できる
サンプルコード
実行結果
next()メソッドに引数を渡すと、generatorに値
を送りこめる
function* hoge(){
var x = null;
console.log('inside generator: x is ', x);
x = yield 1;
console.log('inside generator: after yielded, x is ', x);
}
var iterator = hoge();
console.log('return value of next', iterator.next());
console.log('return value of next', iterator.next('hoge'));
1
2
3
4
5
6
7
8
9
10
11
inside generator: x is null
return value of next { value: 1, done: false }
inside generator: after yielded, x is hoge
return value of next { value: undefined, done: true }
1
2
3
4
https://github.com/craftgear/ong6/tree/master/example/03_next.js
34. Thunkってなに?
A "thunk" is also known as a "continuable"
and is simply a function that accepts a node
style callback as it's only argument.
訳: "thunk"は"continuable"ともいわれ、
node形式のコールバックのみを引数として
け る関数です。
gen-runのREADME.mdより
function sleep(ms) {
return function (callback) {
setTimeout(callback, ms);
};
}
43. nodeの方針
by Isaac
コールバックスタイルの理解は必須
The Future of Programming in Node.js
"Callbacks will remain the de facto way to
implement asynchrony. Generators and
Promises are interesting and will remain a
userland option."
訳: コールバックがデファクトの非同期実
装方法で在り続けます。ジェネリータやプロ
ミスは興味深いですが、ユーザーランドの選
択肢のままです。
50. イテリータとジェネリータ - JavaScript | MDN
generators in v8 -- wingolog
A Study on Solving Callbacks with JavaScript Generators
A Closer Look at Generators Without Promises
jmar777/suspend
visionmedia/co
spion/genny
creationix/gen-run
The Future of Programming in Node.js - Google グループ
Analysis of generators and other async patterns in node