异步.系列:内存泄漏特性

async.series: memory leak of feature?

本文关键字:泄漏 内存 系列 异步      更新时间:2023-09-26

我正在学习使用series.js。我写了一个简单的例子-为3个函数运行async.series。出于好奇,我创建了回调调用前后的日志输出。意想不到的是,我没有日志消息"回调后"。

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

没有'after callback'日志,因为您是从带有'after callback'的行之前的函数返回的:

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');
    };