0

我正在学习使用series.js。我写了一个简单的例子——运行async.series3 个函数。出于好奇,我在回调调用前后创建了日志输出。出乎我意料的是,“回调后”没有日志消息。

我的问题是 - 是内存泄漏,这些调用仍在堆栈中等待返回吗?还是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
4

1 回答 1

2

没有“回调后”日志,因为您是从带有“回调后”行之前的函数返回的:

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');
    };
于 2012-12-31T08:48:40.040 回答