【问题标题】:async.series: memory leak of feature?async.series:功能的内存泄漏?
【发布时间】:2012-12-31 16:48:40
【问题描述】:

我正在学习使用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

【问题讨论】:

  • async 是异步的缩写,因此您看到消息以不同的顺序记录是有道理的……这不是内存泄漏 AFAIK

标签: javascript async.js


【解决方案1】:

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

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

【讨论】:

  • 谢谢。这是之前实验的一部分,我忘了删除它... :-(
猜你喜欢
  • 2010-10-27
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 2020-01-23
相关资源
最近更新 更多