[英]What meaning is return a(function () )?
I studied simple asynchronized JavaScript code, but stuck with return value(function() ). 我研究了简单的异步JavaScript代码,但受返回值(function())困扰。 I don't understand it. 我不明白
How does operate this return value(function())? 如何操作此返回值(函数())?
function _async(func) {
return function () {
arguments[arguments.length++] = function (result) {
_callback(result);
};
(function wait(args){
for (var i = 0; i < args.length; i++) {
if (args[i] && args[i].name == '_async_cb_receiver')
return args[i](function (arg) {
args[i] = arg; wait(args);
})}
func.apply(null, args);
})(arguments);
var _callback;
function _async_cb_receiver(callback) {
_callback = callback;
}
return _async_cb_receiver;
};
}
return args[i](function (arg) {
args[i] = arg; wait(args);
})
I don't understand this part. 我不明白这部分。 wait(args)
just returns args[i]
, but parentheses appear and anonymous function is executed. wait(args)
仅返回args[i]
,但是会出现括号并执行匿名函数。 How does this function handle args[i]
and what arguments are in arg
? 此函数如何处理args[i]
以及arg
哪些参数?
args
is an array of functions. args
是一个函数数组。 So args[i]
is one of these functions, and args[i](something)
calls that function with something
as the argument. 因此args[i]
是这些函数之一,而args[i](something)
则以something
作为参数调用该函数。
In this case, something
is another function. 在这种情况下, something
功能是另一个功能。 This function replaces args[i]
with its own argument, then calls wait(args)
recursively. 此函数用其自己的参数替换args[i]
,然后递归调用wait(args)
。
This code is pretty convoluted, so I'm not sure what it's doing as a whole. 这段代码非常复杂,所以我不确定它的整体功能。 I suspect it's doing something similar to the way promises work, but was written before they were added to the language. 我怀疑它的工作方式与Promise的工作方式类似,但是是在将它们添加到语言之前编写的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.