BT

How Do You Handle Async APIs and Callback Hell?

by Werner Schuster on May 23, 2014 |

While asynchronous APIs can bring advantages, they certainly complicate programming by forcing developers to manually string together sequential operations using callbacks, causing a phenomenon called "Callback Hell".

The increasing popularity of asynchronous APIs, partially due to the rise of JavaScript and Node.js, means that many developers are now faced with a prolonged stay in Callback Hell. As is usual, developers with enough of an itch will find a way to scratch it. In the case of Callback Hell, there's a rich supply of hacks, workarounds, and solutions.

InfoQ wants to know: how do you handle asynchronous APIs? How do you stay out of Callback Hell or how do you manage it?

Language features and related

These are solutions that are either straight up language features or are supported by language features (eg. macros, etc).

Extensions of the language runtime or bytecode

Libraries

  • Reactive Extensions (Rx)
    An increasingly popular approach that started on .NET and has been ported to other platforms including Java and JavaScript. As an example, Netflix is behind the Java port RxJava; see Netflix' Ben Christensen interview on RxJava.
  • Promises or Futures
    Promises (and their namesakes) have been adopted by many (standard) libraries and make it easier to chain operations. 
  • Embedded DSL-style libraries
    Libraries that allow one to chain statements, often using Promises underneath, are particularly popular in JavaScript.

Misc

  • State machines
    Using libraries like ignite.js, Ragel, etc.
  • Just write callbacks!
    Is "Callback Hell" just bad developers complaining about their tools? Do you just grit your teeth and nest your callbacks rather spend weeks researching alternative solutions?
  • Synchronous APIs
    Do you avoid asynchronous APIs at all costs? 

Note: if we missed your favorite language, library or solution, please tell us about it in the comments. 

 

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

What other language with lightweight threads? by Yichao Zhang

If that other language is Erlang, then I think it's quite mature and widely adopted.

Protothreads by John Carter

At the intersection of light weight threads, C, evil abuses of language standards and state machines there exists Protothreads.

dunkels.com/adam/pt/

Law of Demeter by John Carter

Actually, call back hell is a code smell.

It's a stench of a Law of Demeter Violation in progress.

The solution is to Tell, don't Ask.

We use kontraktor :-) by Rüdiger Möller

Re: Law of Demeter by Ted Slusser

I'll Ask you to Tell me more about it :)

bad poll by Rüdiger Möller

You should abstract out general patterns (e.g. actors, csp, traditional multithreading, lightweight mt). It mixes up implementations, language and methodology. There are many terminologies for identical models.

check out this

java-is-the-new-c.blogspot.de/2014/01/comparisi...

java-is-the-new-c.blogspot.de/2014/06/java-conc...

Continuations? by Cameron Purdy

Surprised that "continuations" was not one of the options.

Async API in Java by Zhong Yu

Please also check out bayou Async API - bayou.io/release/0.9/docs/async/Async_Programmi...

Re: Continuations? by Mark Sasler

Take a look at www.dragome.com, this tool is capable of avoiding callback hell using continuations.
Its CallbackEvictor module works creating a proxy over invoked service, it pauses the application flow while is waiting the async reply and continue the execution when internal callback is executed with the expected result.
It allows the use of continuations and dynamic proxies in javascript thanks to an automatic Java to Javascript conversion. Since this conversion is based in generated bytecode and not the source code, libraries such as Javaflow are used to achieved this.



Surprised that "continuations" was not one of the options.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

9 Discuss
General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT