Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Generators in ES6

Generators in ES6

shawn0102

June 07, 2014
Tweet

More Decks by shawn0102

Other Decks in Programming

Transcript

  1. var arr = ['I', 'l', 'o', 'v', 'e', 'u']; !

    var iterator = magicFunction(arr); ! iterator.next(); // => 'I' iterator.next(); // => 'l' iterator.next(); // => 'o' iterator.next(); // => 'v' iterator.next(); // => 'e' iterator.next(); // => 'u'
  2. function *generator(arr) { for (var i in arr) { yield

    arr[i]; } } ! ! var arr = ['I', 'l', 'o', 'v', 'e', 'u']; var iterator = generator(arr); ! console.log(iterator.next().value); console.log(iterator.next().value); console.log(iterator.next().value); console.log(iterator.next().value); console.log(iterator.next().value); console.log(iterator.next().value);
  3. var ticking = true; var clock = function() { if

    (ticking) console.log('Tick!'); else console.log('Tock!'); ticking = !ticking; } function *clock() { while (true) { yield; console.log('Tick!'); yield; console.log('Tock!'); } } use a variable to store the state with coroutine
  4. Coroutines add an entirely new form of state to the

    language ! namely the state of where the coroutine is currently suspended
  5. async.series([ // asycStep1 function (callback){ callback(null, 'one'); }, // asycStep2

    function (callback){ callback(null, 'two'); }, // asycStep3 function (callback){ callback(null, 'two'); }, // asycStep4 function (callback){ callback(null, 'two'); } ], function(err, results){}); with async.js
  6. var step1Result = asyncStep1(); var step2Result = asyncStep2(step1Result); var step3Result

    = asyncStep3(step2Result); var step4Result = asyncStep4(step3Result);
  7. var step1Result = asyncStep1(); var step2Result = asyncStep2(step1Result); var step3Result

    = asyncStep3(step2Result); var step4Result = asyncStep4(step3Result); function *generator() { var step1Result = yield asyncStep1; var step2Result = yield asyncStep2; var step3Result = yield asyncStep3; var step4Result = yield asyncStep4; } Hmm…get closer to our goal! So we can write like this:
  8. var step1Result = asyncStep1(); var step2Result = asyncStep2(step1Result); var step3Result

    = asyncStep3(step2Result); var step4Result = asyncStep4(step3Result); function *coroutine() { var step1Result = yield asyncStep1; var step2Result = yield wrapper(asyncStep2, step1Result); var step3Result = yield wrapper(asyncStep3, step2Result; var step4Result = yield wrapper(asyncStep4, step3Result); } ! wow…even closer to our goal! So we can write like this:
  9. wrapper function • return an async function to be executed

    which have only one param, the callback. • make a closure to pass params to the original async function function wraper(asyncFn, param) { return function(callback) { asyncFn(param, callback); } }
  10. var result = generator.next(); result.value(function(v) { result = generator.next(v); result.value(function(v)

    { result = generator.next(v); result.value(function(v) {}); }); }); function mainRoutine(fn) { var generator = fn(); process(generator.next()); ! function process(result) { if (!result.done) { result.value(function(v) { process(generator.next(v)); }); } } }
  11. mainRoutine(function *() { var step1Result = yield asyncStep1; var step2Result

    = yield wrapper(asyncStep2, step1Result); var step3Result = yield wrapper(asyncStep3, step2Result; var step4Result = yield wrapper(asyncStep4, step3Result); }); Look familiar? Yes, it’s a simple version of co and wrapper function is also a simple version of thunkify. Finally we get this:
  12. Async programming in nodejs is… • You can’t expect async

    programming to disappear, because js is born to be async. • The way we writes async codes differ. • Generator brings us a new way to hide callbacks , just like other libs. • But generator is good at dealing with exceptions.