async.series:内存泄漏功能?

时间:2012-12-31 08:12:11

标签: javascript async.js

我正在学习使用series.js。我写了一个简单的例子 - 运行async.series 3个函数。由于好奇,我在回调调用之前和之后创建了日志输出。对我来说意外的是回拨后没有日志消息'。

我的问题是 - 它是内存泄漏,这些调用仍处于堆栈状态并等待返回?或者async.js在回调后使用特殊机制来切割函数?我试着阅读async.js来源并没有发现任何内容。

有什么想法吗?

测试页面:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8" />
<title>My Page</title> 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/caolan/async/master/lib/async.js"></script>
<script type="text/javascript">

    var a = function (callback) {
        console.log('a before callback');
        return callback(null);
        console.log('a after callback');
    };
    var b = function (callback) {
        console.log('b before callback');
        return callback(null);
        console.log('b after callback');
    };
    var c = function (callback) {
        console.log('c before callback');
        return callback(null);
        console.log('c after callback');
    };

    var doit = function() {
        console.log('click');
        async.series([a, b, c, a, b, c], function(something) {console.log('async.series happy end: '+something);});
        console.log('series finished');
    };

    $(function() {
        $('#bu').click(doit);           
    });

    console.log('hello');
</script>
</head> 
<body id="bo" class="blue">
<input type="button" id="bu" value="click"><br />
</body>
</html>

日志输出:

hello
event.layerX and event.layerY are broken and deprecated in WebKit. They will be removed from the engine in the near future.
click
a before callback
b before callback
c before callback
a before callback
b before callback
c before callback
async.series happy end: null
series finished

1 个答案:

答案 0 :(得分:2)

回调后没有&#39;&#39;记录,因为您在回调&#39;之后使用&#39;

之前的功能返回
var c = function (callback) {
        console.log('c before callback');

        // the next line exits this function and nothing after it will execute
        return callback(null);

        // this won't execute because the function has returned.
        console.log('c after callback');
    };