Remember the callback daze

Javascript is everywhere.. it took off to be the most predominant language that applications are developed on both server (node.js)  &  client side (web, mobile, desktop) with plethora of technologies & frameworks.  While coding/debugging nodejs modules, I understand the asynchronous pattern of callbacks functions and how they fit into the event-based execution model of Javascript. But sometimes, the callbackhell is quite overwhelming and I get lost in the daze of callback functions, which ends up in myself strolling through the blogs for a better, clear understanding of the concept. This post is a synthesis of one such instance.

Coming from a Java/C# Eco system, where traditionally functions are defined as per contract/definition and executed sequentially (well…most of the time, other than EventHandlers, callback interfaces, etc.) & every function/method has an object input, output & possible exception handling. Functions in javascript are different as they can be defined traditionally with input arguments & output/return values. They can also be expressed, manipulated passed around like objects. Functions can be also be returned as local variables (closures) and in strict mode, they bleed ‘undefined’ profusely. All good, then what exactly is an callback ?

Callbacks are functions that are executed asynchronously, or at a later time. Instead of the code reading top to bottom procedurally, async programs may execute different functions at different times based on the order and speed that earlier functions like http requests or file system reads happen. (ref:art-of-node#callbacks)

Best explanation I found:
Mike Vollmer did an fantastic job in explaining what are callbacks & how callbacks are used to do things asynchronously in javascript vs. traditional programming languages.

In addition,The Difference Between Call() and Apply() in Javascript would further clarify operations on callback. (ex: callback-apply.js). Another simple explanation: .

Key points noted are:

  • explicitly making sure callback is a function before use.  (typeof(callback) === ‘function’)
  • callback executes after the asynchronous action begins, but NOT before it completes (Not always) (ex: callback-timing.js).

For well versed javascript coders, it must be pretty basic stuff, but this has been an shift in programming paradigm for me. I do use callbacks and anti-pattern libraries like async.js for node modules & yet sometimes I cannot wrap my head around these callbacks. Modularising code and getting more familiar with asynchronous programming and node.js patterns seems to be solution for the confusion… till then…I need to remember the callback daze..


Tagged with: ,
Posted in JavaScript, Node.JS
One comment on “Remember the callback daze
  1. asoftwareguy says:

    Asyncronous programming can be a bit of a pain when you are first looking into it, but it becomes easlier over time, just like anything else. If you are ever still in Java world, take a look at the reactive extensions for Java,, or in .Net,

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: