jslab. Домников Виталий. "es6 генераторы и koa.js"
TRANSCRIPT
ES6 GENERATORSAND KOA.JS
VITALY DOMNIKOV
GITHUB: @DOTCYPRESS
TWITTER: @DOTCYPRESS
HAPPY JS DEVELOPMENT
fs.readFile(inputFile, function (err, data) { if (err) return res.status(500).send(err); process1(data, function (err, data) { if (err) return res.status(500).send(err); process2(data, function (err, data) { if (err) return res.status(500).send(err); process3(data, function (err, data) { if (err) return res.status(500).send(err); fs.writeFile(outputFile, data, function (err) { if (err) return res.status(500).send(err); res.status(200).send('YEAH'); }); }); }); });});
MIRACLE
//node --harmony sample.js
var sampleGenerator = function *() { yield 1; yield 2;}var generator = sampleGenerator();console.log(generator.next()); // { value: 1, done: false }console.log(generator.next()); // { value: 2, done: false }console.log(generator.next()); // { value: undefined, done: true }
GENERATORS METHODSnext()
This is used to resume the execution.
generator.next();
// Result:{ value: 1, done: false}
GENERATORS METHODSthrow()
This is used to throw an error or exception at anystep.
generator.throw(new Error("BSOD"));
FIBBONACI
var fibbonaciGenerator = function *() { var acc = 1; var result = 0; while (true) { yield result; var temp = result; result += acc; acc = temp; }};
var generator = fibbonaciGenerator();console.log(generator.next()); // { value: 0, done: false }console.log(generator.next()); // { value: 1, done: false }console.log(generator.next()); // { value: 1, done: false }console.log(generator.next()); // { value: 2, done: false }
DELEGATING YIELD
var sampleGenerator = function *() { yield 1; yield 2;}
var delegatedGenerator = function *() { yield* sampleGenerator(); yield 3; yield 4;}var generator = delegatedGenerator();console.log(generator.next()); // { value: 1, done: false }console.log(generator.next()); // { value: 2, done: false }console.log(generator.next()); // { value: 3, done: false }console.log(generator.next()); // { value: 4, done: true }
COGENERATOR BASED CONTROL FLOWGOODNESS FOR NODEJS AND THEBROWSER, USING PROMISES,LETTING YOU WRITE NONBLOCKINGCODE IN A NICEISH WAY.
https://github.com/tj/co
COThe "yieldable" objects currently supported are:
promisesthunks (functions)array (parallel execution)objects (parallel execution)generators (delegation)generator functions (delegation)
CO SAMPLE
var co = require('co');var thunkify = require('thunkify');var request = require('request');var get = thunkify(request.get);
co(function *(){ var a = yield get('http://google.com'); var b = yield get('http://yahoo.com'); var c = yield get('http://cloudup.com'); console.log(a[0].statusCode); console.log(b[0].statusCode); console.log(c[0].statusCode);})();
THUNKIFYTURN A REGULAR NODE FUNCTIONINTO ONE WHICH RETURNS ATHUNK.
https://github.com/tj/nodethunkify
var thunkify = require('thunkify');var fs = require('fs');
var read = thunkify(fs.readFile);
read('package.json', 'utf8')(function(err, str){
});
HELLO WORLD
var koa = require('koa');var app = koa();
app.use(function *(){ this.body = 'Hello World';});
app.listen(3000);
CASCADING
var koa = require('koa');var app = koa();
app.use(function *(next){ var start = new Date; yield next; var ms = new Date - start; this.set('X-Response-Time', ms + 'ms');});
app.use(function *(){ this.body = 'Hello World';});
app.listen(3000);
CONTEXT
app.use(function *(){ this; // is the Context this.request; // is a koa Request. this.response; // is a koa Response. this.req; // nodejs request. this.res; // nodejs response.});
ERROR HANDLING
app.use(function *(next) { try { yield next; } catch (err) { this.app.emit('error', err, this); this.throw(500, 'Houston we have a problem') }});
BATTERIES NOT INCLUDED
RoutingFile ServingCompressionCachingAuthenticationSessionsetc.
KOA middlewares