Aboutme 
•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 MathsPhD
SomecodeI saw 
CodeSchool’sBasecamp 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 gottabe a way!”
The callbackhell 
“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 isimpossible*… 
*of course you could write synchronous C extensions that would provide this, but say goodbye to performance
This isimpossible*… 
*of course you could write synchronous C extensions that would provide this, but say goodbye to performance
Event Queue 
The reliance on callbacks is due to what is arguably JavaScript’s best feature –its event queue. Instead of using subprocessesor threads, it processes events sequentially as they come in. 
Thus, calling I/O MUST delegate back or everything will come to a halt.
Performance 
Concurrentusers 
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
Performance 
Concurrentusers 
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 
PHP
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.
Rememberthis?
This wayitworks!
Looks prettyneat! 
VS
Even nexttoRails! 
VS
Improvespromises, too
Improvespromises, too 
VS
Improvespromises, too 
VS
…andpromisescopes
…andpromisescopes 
VS
…andpromisescopes 
VS
Towrapup 
•‘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 thunkifyor 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-koamigration could not measure any performance difference in terms of requests served and an increase of memory consumption by 10-15 MB. Absolutely worth it!

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

  • 2.
    Aboutme •Several production-gradenode 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 MathsPhD
  • 3.
    SomecodeI saw CodeSchool’sBasecampSearch, Code written in RoR And I thought “Hey, it’s really been some time since I saw such clear code in node. There’s gottabe a way!”
  • 4.
  • 5.
    Can we dobetter? 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.
  • 6.
    Coffee-Script? Much easieron the eyes, the order of the code is still counter-intuitive.
  • 7.
    This isimpossible*… *ofcourse you could write synchronous C extensions that would provide this, but say goodbye to performance
  • 8.
    This isimpossible*… *ofcourse you could write synchronous C extensions that would provide this, but say goodbye to performance
  • 9.
    Event Queue Thereliance on callbacks is due to what is arguably JavaScript’s best feature –its event queue. Instead of using subprocessesor threads, it processes events sequentially as they come in. Thus, calling I/O MUST delegate back or everything will come to a halt.
  • 10.
    Performance Concurrentusers Responsetime Server CPU usage http://blog.loadimpact.com/2013/02/01/node-js-vs-php-using-load-impact-to-visualize-node-js-efficency/ node.js
  • 11.
    Performance Concurrentusers Responsetime Server CPU usage http://blog.loadimpact.com/2013/02/01/node-js-vs-php-using-load-impact-to-visualize-node-js-efficency/ node.js PHP
  • 13.
    So, ES6? Givesus generators. For this discussion, they are like functions that can be paused and continued at a later point in time. Let’s code.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
    Towrapup •‘yield’ makescode 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 thunkifyor 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-koamigration could not measure any performance difference in terms of requests served and an increase of memory consumption by 10-15 MB. Absolutely worth it!