SlideShare a Scribd company logo
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (and CoffeeScript)
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
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.
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!

More Related Content

What's hot (20)

PDF
Node.js - A Quick Tour
Felix Geisendörfer
 
PDF
Using Node.js to Build Great Streaming Services - HTML5 Dev Conf
Tom Croucher
 
PPTX
Promises, Promises
Domenic Denicola
 
PDF
Matthew Eernisse, NodeJs, .toster {webdev}
.toster
 
PPT
JS everywhere 2011
Oleg Podsechin
 
PPTX
Node.js Patterns for Discerning Developers
cacois
 
PPTX
Node.js System: The Approach
Haci Murat Yaman
 
PDF
ECMAScript 6
偉格 高
 
PPT
Building your first Node app with Connect & Express
Christian Joudrey
 
PDF
Ansible 202 - sysarmy
Sebastian Montini
 
PDF
Express node js
Yashprit Singh
 
PDF
Introduction to reactive programming & ReactiveCocoa
Florent Pillet
 
PPT
Server side JavaScript: going all the way
Oleg Podsechin
 
PDF
Talk KVO with rac by Philippe Converset
CocoaHeads France
 
PDF
Non-blocking I/O, Event loops and node.js
Marcus Frödin
 
PDF
Nodejs Explained with Examples
Gabriele Lana
 
PPTX
How NOT to write in Node.js
Piotr Pelczar
 
PDF
Zenly - Reverse geocoding
CocoaHeads France
 
KEY
Streams are Awesome - (Node.js) TimesOpen Sep 2012
Tom Croucher
 
PPTX
JavaScript Engines and Event Loop
Tapan B.K.
 
Node.js - A Quick Tour
Felix Geisendörfer
 
Using Node.js to Build Great Streaming Services - HTML5 Dev Conf
Tom Croucher
 
Promises, Promises
Domenic Denicola
 
Matthew Eernisse, NodeJs, .toster {webdev}
.toster
 
JS everywhere 2011
Oleg Podsechin
 
Node.js Patterns for Discerning Developers
cacois
 
Node.js System: The Approach
Haci Murat Yaman
 
ECMAScript 6
偉格 高
 
Building your first Node app with Connect & Express
Christian Joudrey
 
Ansible 202 - sysarmy
Sebastian Montini
 
Express node js
Yashprit Singh
 
Introduction to reactive programming & ReactiveCocoa
Florent Pillet
 
Server side JavaScript: going all the way
Oleg Podsechin
 
Talk KVO with rac by Philippe Converset
CocoaHeads France
 
Non-blocking I/O, Event loops and node.js
Marcus Frödin
 
Nodejs Explained with Examples
Gabriele Lana
 
How NOT to write in Node.js
Piotr Pelczar
 
Zenly - Reverse geocoding
CocoaHeads France
 
Streams are Awesome - (Node.js) TimesOpen Sep 2012
Tom Croucher
 
JavaScript Engines and Event Loop
Tapan B.K.
 

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

PDF
Introduction to Node.js
Richard Lee
 
KEY
Playing With Fire - An Introduction to Node.js
Mike Hagedorn
 
PDF
FITC - Node.js 101
Rami Sayar
 
ODP
Node js lecture
Darryl Sherman
 
PPTX
introduction to node.js
orkaplan
 
PPTX
Node.js: A Guided Tour
cacois
 
KEY
node.js: Javascript's in your backend
David Padbury
 
KEY
NodeJS
.toster
 
PDF
Nodejs - Should Ruby Developers Care?
Felix Geisendörfer
 
PDF
Server Side Event Driven Programming
Kamal Hussain
 
PPTX
Introduction to Node js
Akshay Mathur
 
PPTX
Introducing Node.js in an Oracle technology environment (including hands-on)
Lucas Jellema
 
PDF
Background processes and tasks in an async world
particlebanana
 
ODP
Node js
hazzaz
 
PDF
NodeJS
LinkMe Srl
 
PDF
Nodejs - A quick tour (v6)
Felix Geisendörfer
 
PDF
NodeJS for Beginner
Apaichon Punopas
 
PDF
How to make a high-quality Node.js app, Nikita Galkin
Sigma Software
 
PPTX
StrongLoop Overview
Shubhra Kar
 
PDF
540slidesofnodejsbackendhopeitworkforu.pdf
hamzadamani7
 
Introduction to Node.js
Richard Lee
 
Playing With Fire - An Introduction to Node.js
Mike Hagedorn
 
FITC - Node.js 101
Rami Sayar
 
Node js lecture
Darryl Sherman
 
introduction to node.js
orkaplan
 
Node.js: A Guided Tour
cacois
 
node.js: Javascript's in your backend
David Padbury
 
NodeJS
.toster
 
Nodejs - Should Ruby Developers Care?
Felix Geisendörfer
 
Server Side Event Driven Programming
Kamal Hussain
 
Introduction to Node js
Akshay Mathur
 
Introducing Node.js in an Oracle technology environment (including hands-on)
Lucas Jellema
 
Background processes and tasks in an async world
particlebanana
 
Node js
hazzaz
 
NodeJS
LinkMe Srl
 
Nodejs - A quick tour (v6)
Felix Geisendörfer
 
NodeJS for Beginner
Apaichon Punopas
 
How to make a high-quality Node.js app, Nikita Galkin
Sigma Software
 
StrongLoop Overview
Shubhra Kar
 
540slidesofnodejsbackendhopeitworkforu.pdf
hamzadamani7
 
Ad

Recently uploaded (20)

PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
July Patch Tuesday
Ivanti
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Ad

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

  • 2. 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
  • 3. 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!”
  • 5. 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.
  • 6. Coffee-Script? Much easier on the eyes, the order of the code is still counter-intuitive.
  • 7. This isimpossible*… *of course you could write synchronous C extensions that would provide this, but say goodbye to performance
  • 8. This isimpossible*… *of course you could write synchronous C extensions that would provide this, but say goodbye to performance
  • 9. 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.
  • 10. 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
  • 11. 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
  • 13. 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.
  • 24. 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!