10
w/o callback Presented by: Khoa Tran Jan 7th 2014

Nodejs wo callback[Khoa Tran]

Embed Size (px)

DESCRIPTION

You can find us at: http://www.meetup.com/JavaScript-Ho-Chi-Minh-City/ https://www.facebook.com/JavaScriptHCMC?ref=hl https://plus.google.com/communities/116105314977285194967

Citation preview

Page 1: Nodejs wo callback[Khoa Tran]

w/o callback Presented by: Khoa Tran

Jan 7th 2014

Page 2: Nodejs wo callback[Khoa Tran]

Agenda

• Why I don’t like callbacks

• Is there good alternative to write async code?

• Generator

• Get rid of callbacks

Page 3: Nodejs wo callback[Khoa Tran]

Why I don’t like callbacks?

1. Lots of nesting functions

2. Variable scope confusion

3. Handle exceptions everywhere

Writing readable code is hard!!!

Page 4: Nodejs wo callback[Khoa Tran]

Is there good alternative?

• Async

• Fiber

• Promise/Q

• Co

Page 5: Nodejs wo callback[Khoa Tran]

Generator • “In computer science, a generator is a special routine that can be

used to control the iteration behaviour of a loop” - From wikipedia

Executor Generator

next()

process val1

yield val1

next() …

yield val2 process val2

val1

val2

Page 6: Nodejs wo callback[Khoa Tran]

Generator in node.js

• Only supported in version 0.11.x and above. For

now, you have to build and install node.js from

Github.

• In addition, execute code with --harmony flag.

Page 7: Nodejs wo callback[Khoa Tran]

Get rid of callbacks

Clone demo source at:

https://github.com/tvkkpt/jsmeetup-demo

Page 8: Nodejs wo callback[Khoa Tran]

What to do next

Join this open-source project:

https://github.com/visionmedia/co

Page 10: Nodejs wo callback[Khoa Tran]

Thank you and HAPPY NEW

YEAR!