我正在学习使用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
最佳答案
没有“回调后”日志,因为您从“回调后”行之前的函数返回:
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');
};
关于javascript - 异步系列 : memory leak of feature?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14099169/