Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (and CoffeeScript)

Post on 13-Jul-2015

1.152 views 0 download

Tags:

Transcript of Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (and CoffeeScript)

About me

• Several production-grade node apps in the wild • muuuf

• Up & Front

• EthPort (using what I show today)

• The others I cannot mention

• Maintainer of named-routes, help out at CoffeeScript, bluebird and sequelize

• Berlin Node.js (550 members)

• Actually a Maths PhD

Some code I saw

CodeSchool’s Basecamp Search, Code written in RoR

And I thought “Hey, it’s really been some time since I saw such clear code in node. There’s gotta be a way!”

The callback hell“best-practice” node-equivalent

Can we do better?Yes, remove error-checking by using promises. Have one error-catcher (domain) outside the controller logic.

Somewhat better - still twists your head reading it. Also, you lose variable scope as you move down the chain.

Coffee-Script?

Much easier on the eyes, the order of the code is still counter-intuitive.

This is impossible*…

*of course you could write synchronous C extensions that would provide this, but say goodbye to performance

This is impossible*…

*of course you could write synchronous C extensions that would provide this, but say goodbye to performance

Event QueueThe reliance on callbacks is due to what is arguably JavaScript’s best feature – its event queue. Instead of using subprocesses or threads, it processes events sequentially as they come in.

Thus, calling I/O MUST delegate back or everything will come to a halt.

PerformanceConcurrent users Response time Server CPU usage

http://blog.loadimpact.com/2013/02/01/node-js-vs-php-using-load-impact-to-visualize-node-js-efficency/

node.js

PerformanceConcurrent users Response time Server CPU usage

http://blog.loadimpact.com/2013/02/01/node-js-vs-php-using-load-impact-to-visualize-node-js-efficency/

node.js

PH

P

So, ES6?

Gives us generators. For this discussion, they are like functions that can be paused and continued at a later point in time.

Let’s code.

Remember this?

This way it works!

Looks pretty neat!

VS

Even next to Rails!

VS

Improves promises, too

Improves promises, too

VS

Improves promises, too

VS

…and promise scopes

…and promise scopes

VS

…and promise scopes

VS

To wrap up• ‘yield’ makes code clearer, better readable and more

maintainable

• Start using this! 0.11.13 runs two of my production websites without any hiccups (0.11.14 has some edge case memory leaks)

• Use co and/or bluebird in all places, transform third-party libraries using thunkify or bluebird’s promisify

• If possible, replace express with koa (also gives you named routes for free)

• Finally, my benchmarks of stress testing a real-life app before and after an express-to-koa migration could not measure any performance difference in terms of requests served and an increase of memory consumption by 10-15 MB. Absolutely worth it!