- Mastering JavaScript Promises
- Muzzamil Hussain
- 612字
- 2021-07-16 13:46:47
The mechanism of a callback function in JavaScript
The question here is, how on earth does a callback function work?
As we know that functions are like first class objects in JS, we can pass them around in a similar way to variables and return them as functions and use them in other functions.
When we pass a callback function as arguments to another function, we are only passing the function definition. We aren't executing functions in parameters. We are also not passing the function with the trailing pair of executing parenthesis ()
, as we would when we are executing a function.
Since the containing function has the callback function in its parameter as a function definition, it can execute the callback at any time.
It is important to note that the callback function is not executed immediately. It is "called back" and can still be accessed later via the arguments object by the containing function.
Basic rules to implement callbacks
There are some basic rules that you need to keep in mind while you are implementing the callbacks.
Callbacks are normally simple, but you should be familiar with the rule if you are crafting your own callback functions. Here are some key pointers that you must take into account while you are working on your callback functions:
- Use named or anonymous functions as callbacks
- Pass parameters to callback functions
- Make sure callback is a function before executing it
Handling callback hell
As JavaScript uses callback functions to handle asynchronous control flow, working with nesting of callbacks can become messy and most of the time, out of control.
One needs to be very careful while writing callbacks or using it from any other library.
Here is what happens if the callbacks are not handled properly:
func1(param, function (err, res)) { func1(param, function (err, res)) { func1(param, function (err, res)) { func1(param, function (err, res)) { func1(param, function (err, res)) { func1(param, function (err, res)) { //do something }); }); }); }); }); });
The preceding situation is commonly referred to as callback hell. This is quite common in JavaScript, which makes the lives of engineers miserable. This also makes the code hard for other team members to understand and hard to maintain for further use. The most drastic of all is that it confuses an engineer, making it hard for him/her to remember where to pass on the control.
Here are the quick reminders for callback hell:
- Never let your function be unnamed. Give your function an understandable and meaningful name. The name must show it's a callback function that is performing certain operations instead of defining an anonymous function in the parameter of the main function.
- Make your code less scary to look at and easier to edit, refactor, and hack on later. Most of the engineers write code in a flow of thought with less focus on beautification of code, which makes it difficult to maintain the code later. Use online tools such as http://www.jspretty.com to add readability to your code.
- Separate your code into modules; don't write all your logic in a single module. Instead, write short meaningful modules so that you can export a section of code that does a particular job. You can then import that module into your larger application. This approach can also help you reuse the code in similar applications, thus making a whole library of your modules.
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.