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.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

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.

ACI image

ACI.dev: Best Open-Source Composio Alternative (AI Agent Tooling)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Star our GitHub!