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

24

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

Page 1: Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (and CoffeeScript)
Page 2: 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

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

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!”

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

The callback hell“best-practice” node-equivalent

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

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.

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

Coffee-Script?

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

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

This is impossible*…

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

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

This is impossible*…

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

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

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.

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

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

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

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

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

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.

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

Remember this?

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

This way it works!

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

Looks pretty neat!

VS

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

Even next to Rails!

VS

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

Improves promises, too

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

Improves promises, too

VS

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

Improves promises, too

VS

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

…and promise scopes

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

…and promise scopes

VS

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

…and promise scopes

VS

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

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!