28.03.15. Одесса. Impact Hub Odessa. Конференция JSLab.
Домников Виталий. "ES6 генераторы и Koa.js"
Доклад про ES6 generators, для чего они нужны и как мы до этого докатились и пример использования в Koa.js
Подробнее:
http://geekslab.co/
https://www.facebook.com/GeeksLab.co
https://www.youtube.com/user/GeeksLabVideo
3. 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');
});
});
});
});
});
12. CO
The "yieldable" objects currently supported are:
promises
thunks (functions)
array (parallel execution)
objects (parallel execution)
generators (delegation)
generator functions (delegation)
13. 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);
})();
17. HELLO WORLD
var koa = require('koa');
var app = koa();
app.use(function *(){
this.body = 'Hello World';
});
app.listen(3000);
18. 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);
19. 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.
});