DEV Community

kavita gupta
kavita gupta

Posted on

Callbacks in JavaScript

Hi All,

I started with season2 of Namaste Javascript Series.
Hope, most of us are already aware of this free series on youtube by @akshaysaini.

Today, I have completed first chapter of season2, and here are my learnings for the same.

What are callbacks.
What are the problems with callbacks.

Callbacks are the functions in JavaScript, which are heavily used to perform async operations. It can be passed as an argument to other function. Since, JavaScript is a synchronous single threaded language. To perform async operations, callbacks play a vital role.

Callbacks are powerful way to achieve async behavior, but there are some limitations with it.

  1. Callback Hell
  2. Inversion of control

0. Callback Hell : In case of callbacks, if there are multiple callbacks hierarchy i.e. nested callbacks. which leads to callback hell. And, sometime, it becomes difficult to manage, read such codes.

1. Inversion of control : When we pass our function to other function, we lose the control of our function. We don't know, how other function is going to use our function.

Hope this article makes sense to all the JavaScript developers.
Please provide your inputs in comments.

Top comments (2)

Collapse
 
smlka profile image
Andrey Smolko

Frankly speaking callbacks are not only for async operations.
E.g. there are list of array prototypes methods - .map(), .filter(), forEach(), etc. where callback is called in a sync manner.

Collapse
 
kavitagupta972 profile image
kavita gupta

Thanks for providing your valuable inputs.