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
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PPTX
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
PDF
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PDF
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PDF
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
Kubernetes - Architecture & Components.pdf
geethak285
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
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!