Image for post
Image for post

Node 7.6 + Koa 2: asynchronous flow control made right

Carlos Vega
Feb 23, 2017 · 5 min read

fs.readdir(source, function (err, files) {
if (err) {
console.log('Error finding files: ' + err)
} else {
files.forEach(function (filename, fileIndex) {
console.log(filename)
gm(source + filename).size(function (err, values) {
if (err) {
console.log('Error identifying file size: ' + err)
} else {
console.log(filename + ' : ' + values)
aspect = (values.width / values.height)
widths.forEach(function (width, widthIndex) {
height = Math.round(width / aspect)
console.log('resizing ' + filename + 'to ' + height + 'x' + height)
this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {
if (err) console.log('Error writing file: ' + err)
})
}.bind(this))
}
})
})
}
})

Enter Koa 2

const koa = require('koa');
const app = new koa();
app.use(function *(next){
var start = new Date;
yield next;
var ms = new Date - start;
console.log('%s %s - %s', this.method, this.url, ms);
});
app.use(function *(){
this.body = 'Hello World';
});

app.listen(3000);
const koa = require('koa');
const app = new koa();
app.use(async (ctx, next) => {
const start = new Date;
console.log(`Started at: ${start}`);
await next();
const ms = new Date - start;
console.log(`Elapsed: ${ms}ms`);
});
app.use(async (ctx) => {
ctx.body = 'Hello world';
});
app.listen(3000);

“Koa middleware are simple functions which return a MiddlewareFunction with signature (ctx, next). When the middleware is run by an "upstream" middleware, it must manually invoke next() to run the "downstream" middleware.” — Koa 2.x Guide

app.use(async (ctx, next) => {
const start = new Date;
console.log(`Started at: ${start}`);
await next();
const ms = new Date - start;
console.log(`Elapsed: ${ms}ms`);
});
...app.use(async(ctx, next) => {
// Step 1
const start = new Date;
console.log(`Started at: ${start}`);
await next(); // Step 2
// Step 7
const ms = new Date - start;
console.log(`Elapsed: ${ms}ms`);
});
app.use(async(ctx, next) => {
// Step 3
console.log(`Hey, I'm just another middleware`);
await next(); // Step 4
// Step 6
console.log('More stuff after body has been set');
});
app.use(async(ctx) => {
const name = await dbCall();
// Step 5
ctx.body = `Hello ${name}`;
console.log('Body has been set');
});
...

Takeaway

Image for post
Image for post
Koa vs Express (https://github.com/koajs/koa/blob/master/docs/koa-vs-express.md)

ninjadevs

A group of talented developers friends located in Costa…

Carlos Vega

Written by

Software engineer in love with web development. Avid reader and occasional blogger. He will blog about anything that crosses his mind. Costa Rica.

ninjadevs

ninjadevs

A group of talented developers friends located in Costa Rica.

Carlos Vega

Written by

Software engineer in love with web development. Avid reader and occasional blogger. He will blog about anything that crosses his mind. Costa Rica.

ninjadevs

ninjadevs

A group of talented developers friends located in Costa Rica.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store