What happens in JavaScript if I never call a callback? - Stack Overflow

Suppose I have the following codefunction myFunction(param, callback) {...if (err) {console.log("e

Suppose I have the following code

function myFunction(param, callback) {
    ...
    if (err) {
        console.log("error");
        console.log(err);
    }
    else {
        callback(data);
    }
}

In the case of no error, the callback is called. In the case of an error, it is not. Suppose the calling function looks something like the following

myFunction(param, function(data) {
...
});

Are there memory leak issues or similar? Is there a better way to handle this scenario?

Suppose I have the following code

function myFunction(param, callback) {
    ...
    if (err) {
        console.log("error");
        console.log(err);
    }
    else {
        callback(data);
    }
}

In the case of no error, the callback is called. In the case of an error, it is not. Suppose the calling function looks something like the following

myFunction(param, function(data) {
...
});

Are there memory leak issues or similar? Is there a better way to handle this scenario?

Share Improve this question asked Mar 28, 2012 at 0:44 deltanovemberdeltanovember 44.1k66 gold badges168 silver badges245 bronze badges 1
  • console.log can be considered a callback too; and the code inside the 'else' block is never executed..so you have an anonymous function defined but not executed. I see no problems in your code. Just a consideration, I'm not a pro – gpasci Commented Mar 28, 2012 at 0:50
Add a ment  | 

3 Answers 3

Reset to default 10

A JavaScript object will not be eligible for reclamation as long as it is strongly-reachable: that is, if it can be reached by traversing the object graph from a root object (which basically amounts to a global property or, possibly closed-over, variable). Any object that is no longer strongly reachable is no longer accessible via JavaScript and will be reclaimed by the GC (when the GC feels like it).

In this case the function-object (callback) passed to myFunction is only strongly-reachable for the duration of the function call when it is accessible via the callback parameter*. Because the function-object is not strongly-reachable after the function (e.g. it was not saved to a global property) then the function-object is eligible for reclamation - along with any function scopes that it referenced should they no longer be strongly-reachable - as soon as the function terminates.

Thus in this case, there is no "memory leak". However, imagine this case:

window.myCallbacks = []
function myFunction(param, callback) {
    ...
    window.myCallbacks.push(callback)  // hmm, maybe always strongly-reachable?
}

Happy coding.


Technically, a real smart JavaScript engine could determine that the object named by callback was no longer strongly-reachable (via callback) in the "if" branch. I am not sure if any of the JS engines actually go this far, but the question bees more interesting when talking about function scopes bound in closures (and if this keeps all the objects named by all the variables, even those not accessed later, strongly-reachable).

No need to worry about leaking memory with out calling a callback. You may want to have an error callback.

Adding to the above answer see: https://developer.mozilla/en-US/docs/Web/JavaScript/Memory_Management. Where it says:

This algorithm reduces the definition of "an object is not needed anymore" to "an object is unreachable".

This algorithm assumes the knowledge of a set of objects called roots (In JavaScript, the root is the global object). Periodically, the garbage-collector will start from these roots, find all objects that are referenced from these roots, then all objects referenced from these, etc. Starting from the roots, the garbage collector will thus find all reachable objects and collect all non-reachable objects.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745395833a4625872.html

相关推荐

  • What happens in JavaScript if I never call a callback? - Stack Overflow

    Suppose I have the following codefunction myFunction(param, callback) {...if (err) {console.log("e

    9小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信