Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js - why anonymous callbacks [closed]

I'm learning node.js, and have noticed that pretty much all callbacks are inlined as anonymous callbacks into the function. Is there a specific reason behind doing things this way?

I think using a named callback, and defining it as a local function has 2 advantages: 1. it's cleaner, and doesn't turn the function into one giant block of code 2. given an appropriate name, it acts as documentation - describing what the callback is supposed to do

like image 734
tldr Avatar asked Jun 14 '13 05:06

tldr


People also ask

What is the problem with callbacks?

Error handling in nested callbacks As you can see, we have to handle error in every single callback and it's actually a repetition of a boilerplate to handle errors. So this code is not DRY (we as programmers don't like to repeat the same code over and over again) and it's kind of difficult to reuse an error handler.

Can you use an anonymous function in a callback?

In JavaScript, callbacks and anonymous functions can be used interchangeably.

How can you avoid callback Hells?

We can avoid the callback hell with the help of Promises. Promises in javascript are a way to handle asynchronous operations in Node. js. It allows us to return a value from an asynchronous function like synchronous functions.

Are callbacks closures?

Callbacks are also closures as the passed function is executed inside other function just as if the callback were defined in the containing function.


1 Answers

When using named functions in the global scope as callbacks, the scope where the function is named may cause the function to persist in memory and prevents it from being garbage collected. This is one of the many ways to cause memory leaks in your application. Anonymous functions on the other hand, are marked for GC immediately after their execution is over, and anything not returned (could be a closure as well) will be auto marked for garbage collection.

Consider a fairly complex jQuery plugin. Before generating and returning the actual object that is the subject of the plugin, it may have to create dozens of variables that hold temporary state data. If this were not done in an IIFE (Immediately invoked function expression: an anonymous function that is immediately executed), these variable would "leak" into the global scope. Data in JavaScript will remain in memory as long as there is any one variable or closure still referencing it. Since these variables have "leaked" to the global scope, they will remain in memory until that tab/window is closed. When defined inside an IIFE, the variables defined are stuck in the local anonymous function's scope. Thus, when the function has completed execution, the variables are "gone" and their data no longer has any references. The JS Engine's garbage collector notices that this specific data in memory is not being referenced anywhere anymore, and marks it for deletion, freeing up the occupied memory for other data.

So if you named your functions and you're calling them only once, it's possible they are needlessly taking up memory. Making them anonymous will reclaim the memory after execution, reducing your app's memory overhead.

This is essentially a description of how most dynamic languages work, and why they are more popular than static languages like C, where you have to keep track of every memory allocation you've made, and make sure you delete them when you no longer need them (an exercise in itself; deciding how long you'll need a particular data is not trivial always).

like image 59
abject_error Avatar answered Sep 28 '22 01:09

abject_error