How to Prevent Callback Hell

Post on 16-Jan-2017

532 views 2 download

Transcript of How to Prevent Callback Hell

How to PreventCallback Hell

Tianyu ZHAO

Self-IntroductionFrom ChinaJoined Drecom 1 year agoRails Developer

What is callback hell?

Why should we deal with it?

Hard to Understand

How to avoid this?

Name your functions

Define new functions

Use Promise object

Use Promise objectCreate custom getJSON function

Use Promise objectUse custom getJSON function

Use Promise objectDon’t have to do that at all (Deferred Object in jQuery 1.8+)http://api.jquery.com/category/deferred-object/

Use Promise + Generator

What’s Generator?

Use Promise + Generator

More about Promise

Use Promise + Generator

More about Promise

Use Promise + Generator

Cool Library: co (https://github.com/tj/co)

yield Promise

Use Promise + Generator

Cool Library: co (https://github.com/tj/co)

Promiseresolver

resolverGenerato

r.next()

Promiseresolver

ConclusionCallback hell is kind of the feature of JavaScriptSome people like it, some people don’t like itDecide how to handle it by oneself (simply name functions or use Promise + Generator)

Thank you!